我有关于如何在单击按钮时将 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);
}
}
}