Java 将jtextfield值从一个类传递到另一个类
我有一个问题,关于如何在单击按钮时将一帧中的jextfield值传递给第二帧中的jtextfield 在下面的示例中,在“净工资”文本字段中输入值后,单击“添加”按钮,该值将传递到第二帧的“净工资”文本字段 第1帧。我试图将actionlistener添加到按钮以获取文本,但如何在第二帧中设置它?如果有人能提供一些建议和帮助,我将不胜感激Java 将jtextfield值从一个类传递到另一个类,java,Java,我有一个问题,关于如何在单击按钮时将一帧中的jextfield值传递给第二帧中的jtextfield 在下面的示例中,在“净工资”文本字段中输入值后,单击“添加”按钮,该值将传递到第二帧的“净工资”文本字段 第1帧。我试图将actionlistener添加到按钮以获取文本,但如何在第二帧中设置它?如果有人能提供一些建议和帮助,我将不胜感激 public class Frame1 extends JFrame{ public Frame1() { JPanel guiPanel = ne
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);
}
}
}
如果Frame1之前存在Frame2的实例
在Frame1
中,在构造函数或mutator方法中传递Frame2
的实例
public Frame1(Frame2 frame2)
{
this.frame2 = frame2;
}
然后在Frame2
中,有一个可以更新值的方法,类似于updatenetweag
public void updateNetWage(String value)
{
nettNameTextField.setText(value);
}
在actionListener
函数中,您只需调用:
frame2.updateNetWage(value);
如果Frame2的实例不可用
您可以为Frame2
定义一个单参数构造函数,这样您就可以使用其中的值创建一个新对象
public Frame2(String netNameValue)
{
nettNameTextField.setText(netNameValue);
}
也许他不应该将完整的Frame1引用传递给构造函数。我更希望他让Frame1实现一个带有特殊get方法的接口,并将该接口传递给Frame2构造函数。这是一个很好的观点,但在我看来,这是一个不必要的扩展,只会使代码更加复杂,因为使用一种程序员可以在几秒钟内得到的方法。