Java 如何将变量数据从一个JFrame传递到另一个JFrame?
假设我在第1帧中有一个文本字段和一个按钮,用户单击按钮后,它将进入下一帧并显示用户在第1帧中输入的内容。我已经在网上搜索过了,但没有一个对我有用 因此,您创建了一个方法,在单击按钮时从文本字段检索值Java 如何将变量数据从一个JFrame传递到另一个JFrame?,java,Java,假设我在第1帧中有一个文本字段和一个按钮,用户单击按钮后,它将进入下一帧并显示用户在第1帧中输入的内容。我已经在网上搜索过了,但没有一个对我有用 因此,您创建了一个方法,在单击按钮时从文本字段检索值 public String getTextFieldValue(){ return textField.getText(); } 然后创建一个扩展框架的框架类,并创建一个接受当前框架或字符串的构造函数。然后在实例化时,传入帧或字符串。Boom,您可以访问它。因此,您可以创建一个方法,在单击
public String getTextFieldValue(){
return textField.getText();
}
然后创建一个扩展框架的框架类,并创建一个接受当前框架或字符串的构造函数。然后在实例化时,传入帧或字符串。Boom,您可以访问它。因此,您可以创建一个方法,在单击按钮时从文本字段检索值
public String getTextFieldValue(){
return textField.getText();
}
然后创建一个扩展框架的框架类,并创建一个接受当前框架或字符串的构造函数。然后在实例化时,传入帧或字符串。Boom,您可以访问它。请尝试以下操作:
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());
}
}
}
请尝试以下操作:
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());
}
}
}
使用setter。或者使用CardLayout。如果您无法让它工作,请发布一个和一个更具体的技术问题。请发布您的代码,说明您迄今为止使用setter所做的尝试。或者使用CardLayout。如果你不能让它工作,张贴一个和一个更具体的技术问题。请张贴你的代码,你已经尝试了