在内部帧之间切换

时间:2013-06-07 10:58:44

标签: java swing panels

我正在尝试开发一个简单的项目。我需要更改JInternalFrame内定义的面板的内容。代码如下:

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 :(按下OK按钮后我需要显示的页面)

/*
 * 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                   
}

我是Java新手;请详细说明。

2 个答案:

答案 0 :(得分:2)

在这个简短的完整example中,javax.swing.Action用于通过内部框架的setSelected()方法从菜单中选择框架。您可以引用List<MyFrame>成员变量中的任何框架。

答案 1 :(得分:2)

  

我需要更改JInternalFrame

中定义的面板的内容

该声明暗示您已经有一个可见的JInternalFrame。您发布的代码没有显示JInternalFrame的任何代码,因此我假设您只想在按钮单击时交换面板。

阅读How to Use Card Layout上的Swing教程中的部分。这显示了如何交换面板,无论您使用的是JInternalFrame还是JFrame。