0

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

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

第 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);                      
        }       
}                 
}
4

1 回答 1

0

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

In ,在构造函数或 mutator 方法中Frame1传递 in 的实例。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);
}
于 2013-08-01T08:45:09.453 回答