在内部帧之间切换



我正在尝试开发一个简单的项目。我需要更改在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新手;

我需要更改JInternalFrame

中定义的面板的内容

该语句暗示您已经有了一个可见的JInternalFrame。你发布的代码没有显示任何JInternalFrame的代码,所以我假设你只是想在一个按钮点击上交换面板。

阅读Swing教程中关于如何使用卡片布局的部分。下面展示了如何切换面板,无论您使用的是JInternalFrame还是JFrame。

在这个简短而完整的示例中,javax.swing.Action用于通过内部框架的setSelected()方法从菜单中选择框架,如下所示。您可以引用List<MyFrame>成员变量中的任何帧。相关的例子可以在这里找到

相关内容

  • 没有找到相关文章

最新更新