Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java CardLayout中,如何在卡之间传递数据_Java_Swing_Cardlayout - Fatal编程技术网

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

在Java CardLayout中,如何在卡之间传递数据,java,swing,cardlayout,Java,Swing,Cardlayout,我对使用JavaGUI还不熟悉,我知道这听起来很可笑,但我已经尝试了几天在CardLayout上的卡之间传递数据。我使用的是netbeans,第一张卡显示一个客户列表。选择客户机时,选择将传递给该卡上的变量。下一张卡片查询数据库,以显示有关所选客户端的更多详细信息。我可以处理卡之间的切换,但我的问题是我无法将存储在卡1上的变量中的数据传递到卡2 我已经访问了几个论坛,阅读了类似的问题,但我就是无法找到任何可行的解决方案。请帮帮我,我是新手,所以请对我放松点,谢谢 下面是保存面板的类 public

我对使用JavaGUI还不熟悉,我知道这听起来很可笑,但我已经尝试了几天在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
}
最后是一个类,我需要将客户机选择的信息传输到该类,DSI将向该类显示更多详细信息

  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;


}
公共类clientdetails扩展了javax.swing.JPanel{
公共客户端详细信息(){
初始化组件();
}
公共静态void main(字符串[]args){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(addclient.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
JFrame f=新的JFrame(“窗口”);
f、 添加(新的clientdetails());
f、 设置大小(500700);
f、 setVisible(真);
} 
私有javax.swing.JPanel jPanel1;
}

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

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

我也在想,你可以使用某种模型或映射,但对于你想要实现的目标来说,这可能太过分了。

谢谢大家,我最终使用了一个类容器来保存我需要传递的变量

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

再次感谢您的反馈,我非常感谢。

您好,我已经用我的代码更新了帖子。我尝试将viewclientdetails类的实例传递给cleintdetails构造函数,以使我能够引用该实例中的变量。但是当我实现JFrame f=newjframe(“窗口”);f、 添加(新的clientdetails());第二行给出了一个错误,类clientdetails中的构造函数clientdetails不能应用于给定的类型。我仍然需要你的帮助。Thanks我认为解决问题的方法是使用模型-视图-控件设计模式将程序逻辑与其显示分开。您的“卡片”应该侦听模型中的更改,可能使用PropertyChangeListeners,然后通过更改它们显示的数据来响应更改。不,您不应该为此使用单例。坏习惯-1。
varcontainer.getInstance().variablename