将jtextfield值从一个类传递到另一个类

时间:2013-08-01 08:36:49

标签: java

我有一个问题,关于如何在点击按钮时将jextfield的值从一帧传递到第二帧中的jtextfield。

下面的示例,在nett工资文本字段中输入值后,单击添加按钮后,该值将被传递到第二帧的nett工资文本字段。

第1帧。我尝试将actionlistener添加到我的按钮以获取文本,但是如何在第二帧中设置它?感谢是否有人可以提供一些建议和帮助。

public class Frame1 extends JFrame{
public Frame1() {

    JPanel guiPanel = new JPanel(new GridBagLayout());

    JLabel Nett = new JLabel("Nett Wage: ");
    final JTextField nettNameTextField = new JTextField(10);
    JButton addButton = new JButton("Add");


    JPanel fields = new JPanel(new GridBagLayout());

    GridBagConstraints labelGBC = new GridBagConstraints();
    labelGBC.insets = new Insets(10, 3, 3, 3);
    GridBagConstraints fieldGBC = new GridBagConstraints();
    fieldGBC.insets = new Insets(10, 3, 3, 3);
    GridBagConstraints titleGBC = new GridBagConstraints();
    fieldGBC.gridwidth = GridBagConstraints.REMAINDER;


    fields.add(Nett, labelGBC);
    fields.add(nettNameTextField, fieldGBC);


    JPanel buttons = new JPanel(new GridBagLayout());

    GridBagConstraints addButtonGBC = new GridBagConstraints();
    addButtonGBC.insets = new Insets(40, 3, 3, 3);
    cancelButtonGBC.gridwidth = GridBagConstraints.REMAINDER;
    buttons.add(addButton, addButtonGBC);


    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    guiPanel.add(fields, gbc);
    guiPanel.add(buttons, gbc);


    add(guiPanel, BorderLayout.CENTER);

    /*addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String value = nettNameTextField.getText();

        }
    });*/

第2帧。如何在文本字段中设置值?

public class Frame2 extends JFrame {
public Frame2() {


    JPanel guiPanel = new JPanel(new GridBagLayout());


    JLabel nett = new JLabel("Nett Wage: ");
    JTextField nettNameTextField = new JTextField(10);


    JPanel fields = new JPanel(new GridBagLayout());


    GridBagConstraints labelGBC = new GridBagConstraints();
    labelGBC.insets = new Insets(10, 3, 3, 3);
    GridBagConstraints fieldGBC = new GridBagConstraints();
    fieldGBC.insets = new Insets(10, 3, 3, 3);
    //GridBagConstraints titleGBC = new GridBagConstraints();
    fieldGBC.gridwidth = GridBagConstraints.REMAINDER;




    JPanel savingspanel = new JPanel(new GridBagLayout());

    GridBagConstraints totallabelsGBC = new GridBagConstraints();
    totallabelsGBC.insets = new Insets(10, 3, 3, 3);
    GridBagConstraints totalfieldGBC = new GridBagConstraints();
    totalfieldGBC.insets = new Insets(10, 3, 3, 3);
    totalfieldGBC.gridwidth = GridBagConstraints.REMAINDER;
    savingspanel.add(nett, labelGBC);
    savingspanel.add(nettNameTextField, fieldGBC);



    add(guiPanel, BorderLayout.CENTER);                      
        }       
}                 
}

1 个答案:

答案 0 :(得分:0)

如果Frame1的实例存在于Frame1之前

Frame1中,在构造函数或mutator方法中传递Frame2的实例。

public Frame1(Frame2 frame2)
{
    this.frame2 = frame2;
}

然后在Frame2中,有一个可以更新值的方法,例如updateNetWage

public void updateNetWage(String value)
{
    nettNameTextField.setText(value);
}

actionListener功能中,您只需致电:

frame2.updateNetWage(value);

如果Frame2的实例不可用

您可以为Frame2定义一个参数构造函数,这样就可以创建一个包含值的新对象。

public Frame2(String netNameValue)
{
    nettNameTextField.setText(netNameValue);
}