0

我是使用 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;


}
4

2 回答 2

3

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

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

我还认为您可以使用某种模型 or Map,但这对于您想要实现的目标来说可能是矫枉过正...

于 2013-01-25T03:43:54.157 回答
-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

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

于 2013-01-25T12:25:16.203 回答