假设我在第 1 帧中有一个文本字段和一个按钮,在用户单击按钮后,它将继续到下一帧并显示用户在第 1 帧中输入的内容。我在网上搜索过,但没有它对我有用。
最佳答案
尝试以下操作:
Frame1.java
public class Frame1 extends JFrame{
protected static Frame2 frame2;
protected JTextField textField = new JTextField();
protected JButton button = new JButton("Button");
public Frame1() {
JFrame Frame1 = new JFrame();
Frame1.setBounds(0, 0, 200, 100);
Frame1.setVisible(true);
Frame1.add(textField, BorderLayout.NORTH);
Frame1.add(button, BorderLayout.CENTER);
button.addActionListener(
newButtonListener(
textField,frame2.textField));
}
public static void main(String[] args) {
frame2 = new Frame2();
new Frame1();
}
}
Frame2.java
public class Frame2 {
protected JTextField textField = new JTextField();
public Frame2() {
JFrame Frame1 = new JFrame();
Frame1.setBounds(200, 0, 200, 100);
Frame1.setVisible(true);
Frame1.add(textField, BorderLayout.CENTER);
}
}
ButtonListener.java
public class ButtonListener implements ActionListener{
protected JTextField textField;
protected JTextField textField2;
public ButtonListener(JTextField textField, JTextField textField2) {
this.textField = textField;
this.textField2 = textField2;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Button")) {
System.out.println(textField.getText());
textField2.setText(textField.getText());
}
}
}
关于java - 如何将变量数据从 JFrame 传递到另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29947211/