[英]Passing jtextfield value from one class to another
i have question regarding how to pass value of the jextfield from one frame to the jtextfield in the second frame upon click on a button. 我有一个关于如何单击按钮将jextfield的值从一帧传递到第二帧的jtextfield的问题。
The example below, after inputting a value in the nett wage textfield, upon clicking the add button, the value will be passed on to the nett wage textfield of the second frame. 下面的示例在净工资文本字段中输入值之后,单击添加按钮,该值将传递到第二帧的净工资文本字段中。
Frame 1. I tried to add actionlistener to my button to get text, but how do i set it in the second frame? 框架1.我试图在按钮上添加actionlistener以获取文本,但是如何在第二个框架中设置它呢? Appreciate if anyone can offer some advice and help. 感谢任何人都可以提供一些建议和帮助。
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();
}
});*/
Frame 2. how do i go about setting the value in the textfield here? 框架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);
}
}
}
If an instance of Frame2 exists before Frame1 如果在Frame1之前存在Frame2的实例
In Frame1
, pass an instance of Frame2
in, in either the constructor or in a mutator method. 在Frame1
中,在构造函数或mutator方法中传递Frame2
的实例。
public Frame1(Frame2 frame2)
{
this.frame2 = frame2;
}
Then in Frame2
, have a method that can update the value, something like updateNetWage
. 然后在Frame2
,有一个可以更新值的方法,例如updateNetWage
。
public void updateNetWage(String value)
{
nettNameTextField.setText(value);
}
In your actionListener
function, you can then simply call: 然后,您可以在actionListener
函数中简单地调用:
frame2.updateNetWage(value);
If an instance of Frame2 is not available 如果Frame2的实例不可用
You can define a one parameter constructor for Frame2
, so you can create a new object with the value in it. 您可以为Frame2
定义一个参数构造Frame2
,因此您可以创建一个包含值的新对象。
public Frame2(String netNameValue)
{
nettNameTextField.setText(netNameValue);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.