如何在Java CardLayout中的卡之间传递数据

时间:2013-01-25 03:34:16

标签: java swing cardlayout

我是使用Java GUI的新手,我知道这可能听起来很荒谬,但我已经尝试了几天在CardLayout布局上的卡之间传递数据。我使用netbeans,第一张卡显示列表客户端。选择客户端后,选择选项将传递给该卡上的变量。下一张卡查询数据库以显示有关所选客户端的更多详细信息。我可以处理卡之间的切换,但我的问题是我无法将存储在卡1上的变量中的数据传递给卡2。

我访问了几个论坛并阅读了类似的问题,但我无法获得任何提议的解决方案。请帮助,我是新来的,所以请放轻松我,谢谢。

这是包含面板的类

public class mainframe extends javax.swing.JFrame {

public mainframe() {
    initComponents();
}



@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    displayScrollPane = new javax.swing.JScrollPane();
    displaypanel = new javax.swing.JPanel();
    viewclientspanel1 = new viewclientspanel();
    addclientpanel1 = new addclientpanel();
    clientdetails1 = new clientdetails();
    mainmenu = new javax.swing.JMenuBar();
    clients = new javax.swing.JMenu();
    viewclients = new javax.swing.JMenuItem();
    addclient = new javax.swing.JMenuItem();
    transactions = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    displaypanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    displaypanel.setLayout(new java.awt.CardLayout());

    viewclientspanel1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            viewclientspanel1MouseClicked(evt);
        }
    });
    displaypanel.add(viewclientspanel1, "viewclientscard");
    displaypanel.add(addclientpanel1, "addclientcard");
    displaypanel.add(clientdetails1, "clientdetailscard");

    displayScrollPane.setViewportView(displaypanel);

    clients.setText("Clients");

    viewclients.setText("View Clients");
    viewclients.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            viewclientsActionPerformed(evt);
        }
    });
    clients.add(viewclients);
    viewclients.getAccessibleContext().setAccessibleParent(mainmenu);

    addclient.setText("Add Client");
    addclient.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addclientActionPerformed(evt);
        }
    });
    clients.add(addclient);

    mainmenu.add(clients);

    transactions.setText("Transactions");
    mainmenu.add(transactions);

    setJMenuBar(mainmenu);

    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(displayScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 1059, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(99, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(66, Short.MAX_VALUE)
            .addComponent(displayScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 570, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(33, 33, 33))
    );

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

private void addclientActionPerformed(java.awt.event.ActionEvent evt) {                                          
    CardLayout card = (CardLayout) displaypanel.getLayout();
    card.show(displaypanel, "addclientcard");
}                                         

private void viewclientsActionPerformed(java.awt.event.ActionEvent evt) {                                            
    CardLayout card = (CardLayout) displaypanel.getLayout();
    card.show(displaypanel, "viewclientscard");
}                                           

private void viewclientspanel1MouseClicked(java.awt.event.MouseEvent evt) {
    //viewclientspanel1.getComponentListeners();
}


/**
 * @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(mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(mainframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(mainframe.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 mainframe().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JMenuItem addclient;
private addclientpanel addclientpanel1;
private clientdetails clientdetails1;
private javax.swing.JMenu clients;
private javax.swing.JScrollPane displayScrollPane;
private javax.swing.JPanel displaypanel;
private javax.swing.JMenuBar mainmenu;
private javax.swing.JMenu transactions;
private javax.swing.JMenuItem viewclients;
private viewclientspanel viewclientspanel1;
// End of variables declaration
}

显示可供选择的客户列表的卡类有一个鼠标事件监听器,它获取所选客户端的值并切换下一张卡

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
   CardLayout card = (CardLayout) jPanel1.getParent().getParent().getParent().getParent().getLayout();
   card.show(jPanel1.getParent().getParent().getParent().getParent(), "clientdetailscard");

}

最后,我需要传输客户端所选信息的类,其中包含更多详细信息。

  public class clientdetails extends javax.swing.JPanel {


public clientdetails() {

    initComponents();
}

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(addclient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    JFrame f = new JFrame("Window"); 
    f.add(new clientdetails()); 
    f.setSize(500, 700); 
    f.setVisible(true); 
} 

private javax.swing.JPanel jPanel1;


}

2 个答案:

答案 0 :(得分:3)

您可以使用setter / getters将客户端信息从一个窗格传递到另一个窗格...

clientDetailsPane.setClient(clientListPanel.getClient());
// Switch panels...

我也在想你可以使用某种模型或Map,但这对你想达到的目标来说可能有点过分......

答案 1 :(得分:-1)

非常感谢,我最终使用了一个类容器来保存我需要转移的变量。

public class varcontainer {

    public String variablename;

    private static varcontainer instance = null;

    public static varcontainer getInstance(){
    if(instance == null){
        instance = new varcontainer();
    }
        return instance;
    }

}

然后我从另一个类调用getInstance来获取容器的当前实例并访问变量

varcontainer.getInstance().variablename

再次感谢您的反馈,我很感激。