简体   繁体   English

如何在Java CardLayout中的卡之间传递值...这次我给出了一个很好的例子

[英]How do I pass values between cards in Java CardLayout…this time i have given a good example

This is a simple code to clarify my question: 这是一个简单的代码来澄清我的问题:

this is the main class from where I declared the two panels. 这是我宣布两个面板的主要类。 /* * To change this license header, choose License Headers in Project Properties. / * *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 * To change this template file, choose Tools | *要更改此模板文件,请选择“工具”| Templates * and open the template in the editor. 模板*并在编辑器中打开模板。 */ * /

/** * * @author sakib */ public class MainPane { / ** * * @author sakib * / public class MainPane {

private JPanel contentPane;
private Firstcard1 p1;
private SecondCard1 p2;




public void displayGUI()
{
    JFrame frame = new JFrame("Card Layout Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();
    //a=S1;

    contentPane.setLayout(new CardLayout());

    p1 = new Firstcard1(contentPane);
    p2 = new SecondCard1(contentPane);

    contentPane.add(p1, "Panel 1");
    contentPane.add(p2, "Panel 2");

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new MainPane().displayGUI();
        }
    }

    );   

}

} }

this is the first card: 这是第一张牌:

public class Firstcard1 extends javax.swing.JPanel { public class Firstcard1扩展javax.swing.JPanel {

private JPanel contentpane;
/**
 * Creates new form Firstcard1
 */
public Firstcard1(JPanel cp) {
    this.contentpane=cp;
    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() {

    jTextField1 = new javax.swing.JTextField();
    Login = new javax.swing.JButton();

    Login.setText("Login");
    Login.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            LoginActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(117, 117, 117)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(158, 158, 158)
                    .addComponent(Login)))
            .addContainerGap(145, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(105, 105, 105)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(Login)
            .addContainerGap(134, Short.MAX_VALUE))
    );
}// </editor-fold>                        

private void LoginActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    CardLayout layout = (CardLayout)contentpane.getLayout();
    layout.show(contentpane, "Panel 2");
}                                     


// Variables declaration - do not modify                     
private javax.swing.JButton Login;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

} }

this is the second card: 这是第二张牌:

public class SecondCard1 extends javax.swing.JPanel { 公共类SecondCard1扩展javax.swing.JPanel {

private JPanel contentpane;
/**
 * Creates new form SecondCard1
 */
public SecondCard1(JPanel cp) {
    this.contentpane=cp;
    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() {

    jTextField1 = new javax.swing.JTextField();
    back = new javax.swing.JButton();

    back.setText("Back");
    back.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            backActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(127, 127, 127)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(157, 157, 157)
                    .addComponent(back)))
            .addContainerGap(137, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(121, 121, 121)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(back)
            .addContainerGap(102, Short.MAX_VALUE))
    );
}// </editor-fold>                        

private void backActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
     CardLayout layout = (CardLayout)contentpane.getLayout();
    layout.show(contentpane, "Panel 1");
}                                    


// Variables declaration - do not modify                     
private javax.swing.JButton back;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

} }

My question is.if I put a value in the textarea of the first card and press the login button.the value will appear in the text area of the second card.how can i do it? 我的问题是。如果我在第一张卡片的textarea中输入一个值并按下登录按钮。该值将出现在第二张卡片的文本区域。我可以这样做吗?

You somehow have to pass a reference of the second panel to the first one: 你不得不将第二个面板的引用传递给第一个面板:

Main class: 主要课程:

public class MainPane 
{
    public void displayGUI()
    {
        ....
        p1 = new Firstcard1(contentPane);
        p2 = new SecondCard1(contentPane);

        p1.setSecondCard(p2);
        ....
    }
}

First card: 第一张牌:

public class Firstcard1 extends javax.swing.JPanel {
    ....

    private SecondCard secondCard;

    void setSecondCard(SecondCard secondCard)
    { 
        this.secondCard = secondCard;
    }

    ....

    private void LoginActionPerformed(java.awt.event.ActionEvent evt)
    {                                      
        CardLayout layout = (CardLayout)contentpane.getLayout();
        layout.show(contentpane, "Panel 2");
        secondCard.getTextField().setText(jTextField1.getText());
    }                                 
}

Second card: 第二张牌:

public class SecondCard1 extends javax.swing.JPanel 
{
    ...
    JTextField getTextField()
    {
        return jTextField1;
    }
    ...
}

But when you continue like this, the program will probably become a mess pretty soon. 但是当你继续这样的时候,程序很快就会变成一团糟。 I'd recommend to create these components manually, without using any visual GUI builders. 我建议手动创建这些组件,而不使用任何可视化GUI构建器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM