Switching between internal frames

2019-09-09 02:03发布

问题:

I'm trying to develop a simple project. I need to change the contents of the panel defined inside a JInternalFrame. The code is as follows:

JFrame_main:

public class JFrame_main extends javax.swing.JFrame
{

/**
 * Creates new form JFrame_main
 */
public JFrame_main()
{
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents()
{

    basepanel = new javax.swing.JPanel();
    lbl_name = new javax.swing.JLabel();
    bt_clear = new javax.swing.JButton();
    lbl_title = new javax.swing.JLabel();
    txt_name = new javax.swing.JTextField();
    txt_cardno = new javax.swing.JTextField();
    lbl_cardno = new javax.swing.JLabel();
    lbl_accesscode = new javax.swing.JLabel();
    txt_accesscode = new javax.swing.JPasswordField();
    bt_ok = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setPreferredSize(new java.awt.Dimension(839, 480));

    basepanel.setPreferredSize(new java.awt.Dimension(839, 480));

    lbl_name.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    lbl_name.setText("NAME");

    bt_clear.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    bt_clear.setText("CLEAR");
    bt_clear.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            bt_clearActionPerformed(evt);
        }
    });

    lbl_title.setFont(new java.awt.Font("Times New Roman", 1, 30)); // NOI18N
    lbl_title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    lbl_title.setText("AUTOMATED GOVERNMENT RATION SHOP");

    txt_name.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

    txt_cardno.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

    lbl_cardno.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    lbl_cardno.setText("CARD NO.");

    lbl_accesscode.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    lbl_accesscode.setText("ACCESS CODE");

    txt_accesscode.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    txt_accesscode.setMinimumSize(new java.awt.Dimension(6, 26));

    bt_ok.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    bt_ok.setText("OK");
    bt_ok.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            bt_okActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout basepanelLayout = new javax.swing.GroupLayout(basepanel);
    basepanel.setLayout(basepanelLayout);
    basepanelLayout.setHorizontalGroup(
        basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(basepanelLayout.createSequentialGroup()
            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(basepanelLayout.createSequentialGroup()
                    .addGap(96, 96, 96)
                    .addComponent(lbl_title))
                .addGroup(basepanelLayout.createSequentialGroup()
                    .addGap(226, 226, 226)
                    .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, basepanelLayout.createSequentialGroup()
                            .addGap(15, 15, 15)
                            .addComponent(bt_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(79, 79, 79)
                            .addComponent(bt_clear, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(basepanelLayout.createSequentialGroup()
                            .addComponent(lbl_name)
                            .addGap(101, 101, 101)
                            .addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(basepanelLayout.createSequentialGroup()
                            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lbl_cardno)
                                .addComponent(lbl_accesscode))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txt_accesscode, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txt_cardno, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))))))
            .addContainerGap(99, Short.MAX_VALUE))
    );
    basepanelLayout.setVerticalGroup(
        basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(basepanelLayout.createSequentialGroup()
            .addGap(77, 77, 77)
            .addComponent(lbl_title)
            .addGap(61, 61, 61)
            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(basepanelLayout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(lbl_name))
                .addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(txt_cardno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lbl_cardno, javax.swing.GroupLayout.Alignment.TRAILING))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(txt_accesscode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lbl_accesscode))
            .addGap(53, 53, 53)
            .addGroup(basepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(bt_ok)
                .addComponent(bt_clear))
            .addGap(0, 126, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(basepanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(basepanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void bt_clearActionPerformed(java.awt.event.ActionEvent evt)                                         
{                                             
    // TODO add your handling code here:
    txt_name.setText("");
    txt_cardno.setText("");
    txt_accesscode.setText("");
}                                        

private void bt_okActionPerformed(java.awt.event.ActionEvent evt)                                      
{                                          
    // TODO add your handling code here:
    if (authen())
    {
        JPanel_menu menu = new JPanel_menu();
        changepanel(menu);
    }
}                                     

/**
 * @param args the command line arguments
 */
public static void main(String args[])
{
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try
    {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
        {
            if ("Nimbus".equals(info.getName()))
            {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    }
    catch (ClassNotFoundException ex)
    {
        java.util.logging.Logger.getLogger(JFrame_main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (InstantiationException ex)
    {
        java.util.logging.Logger.getLogger(JFrame_main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (IllegalAccessException ex)
    {
        java.util.logging.Logger.getLogger(JFrame_main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (javax.swing.UnsupportedLookAndFeelException ex)
    {
        java.util.logging.Logger.getLogger(JFrame_main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            new JFrame_main().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JPanel basepanel;
private javax.swing.JButton bt_clear;
private javax.swing.JButton bt_ok;
private javax.swing.JLabel lbl_accesscode;
private javax.swing.JLabel lbl_cardno;
private javax.swing.JLabel lbl_name;
private javax.swing.JLabel lbl_title;
private javax.swing.JPasswordField txt_accesscode;
private javax.swing.JTextField txt_cardno;
private javax.swing.JTextField txt_name;
// End of variables declaration                   

//Verifying the USER
boolean authen()
{
    //Add the database query for user verification
    return true;
}

private void changepanel(Component objectname)
{
    basepanel.removeAll();
    basepanel.add(objectname);
    basepanel.repaint();
    basepanel.validate();
}
}

JPanel_menu: (The page i need to display after pressing OK button)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ratm.UI;

/**
 *
 * @author Dinesh Prasanth
 */
public class JPanel_menu extends javax.swing.JPanel
{

/**
 * Creates new form JPanel_menu
 */
public JPanel_menu()
{
    System.out.print("ENTERED");
    initComponents();
    setVisible(true);
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents()
{

    jButton1 = new javax.swing.JButton();

    jButton1.setText("jButton1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(325, 325, 325)
            .addComponent(jButton1)
            .addContainerGap(441, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(239, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(218, 218, 218))
    );
}// </editor-fold>                        
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
// End of variables declaration                   
}

I'm new to Java; please give the changes in detail.

回答1:

I need to change the contents of the panel defined inside a JInternalFrame

That statement implies you already have a visible JInternalFrame. Your posted code does not show any code for a JInternalFrame so I will assume you just want to swap panels on a button click.

Read the section from the Swing tutorial on How to Use Card Layout. This shows how to swap panels whether you are using a JInternalFrame or a JFrame.



回答2:

In this short, complete example, a javax.swing.Action is used to select frames from a menu via the internal frame's setSelected() method. You can reference any frame in the List<MyFrame> member variable.