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

标签 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;


}

最佳答案

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

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

我还认为您可以使用某种模型或Map,但这对于您想要实现的目标来说可能有点过分了......

关于java - 如何在 Java CardLayout 中的卡片之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514912/

相关文章:

java - Gwt-查询 : get an Image from ImageData

java - java excel 提取器的问题

java - 什么更快 : to create a new array or iterate through existing?

java - 来自 SwingX 的 JXCollapsiblePane,无法正常工作

java - 检测 JTextArea 中的换行

java - 尝试让cardLayout工作

java - 将共享项目添加到库时,使用 netbeans 6.8 的 java 应用程序中出现符号错误

java - 带有日期和时间的 JFormattedTextField

java - JPanels 不会在cardLayout 中切换