Swing 在Java中,如何通过单击按钮将值JFrame传递给JFrame?
我需要通过单击按钮的Swing 在Java中,如何通过单击按钮将值JFrame传递给JFrame?,swing,jframe,Swing,Jframe,我需要通过单击按钮的将值从JFrame传递到另一个JFrame。我是编程新手。我什么都试过了。我不知道如何做到这一点 这是我的密码- import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.FlowLayout; public class Demo{ private final JLa
将值从JFrame
传递到另一个JFrame
。我是编程新手。我什么都试过了。我不知道如何做到这一点
这是我的密码-
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.FlowLayout;
public class Demo{
private final JLabel showInput;
private final JTextField inputField;
public Demo(){
JFrame inputFrame = new JFrame("Input");
JFrame outputFrame = new JFrame("Output");
inputField = new JTextField(15);
JButton button = new JButton("Click here");
}
});
showInput = new JLabel("Type something in the box and press the button");
inputFrame.getContentPane().setLayout(new FlowLayout());
inputFrame.getContentPane().add(inputField);
inputFrame.getContentPane().add(button);
inputFrame.pack();
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputFrame.getContentPane().setLayout(new FlowLayout());
outputFrame.getContentPane().add(showInput);
outputFrame.pack();
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputFrame.setVisible(true);
outputFrame.setVisible(true);
}
public static void main(String[] args){
Demo example = new Demo();
}
}
试试这个
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Demo{
private final JLabel showInput;
private final JTextField inputField;
public Demo(){
JFrame inputFrame = new JFrame("Input");
JFrame outputFrame = new JFrame("Output");
inputField = new JTextField(15);
JButton button = new JButton("Click here");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String textFieldText = inputField.getText();
showInput.setText(textFieldText);
}
});
showInput = new JLabel("Type something in the box and press the button");
inputFrame.getContentPane().setLayout(new FlowLayout());
inputFrame.getContentPane().add(inputField);
inputFrame.getContentPane().add(button);
inputFrame.pack();
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputFrame.getContentPane().setLayout(new FlowLayout());
outputFrame.getContentPane().add(showInput);
outputFrame.pack();
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputFrame.setVisible(true);
outputFrame.setVisible(true);
}
public static void main(String[] args){
Demo example = new Demo();
}
}
我添加这些代码以获得所需的函数
/*
button.addActionListener(new ActionListener(){
// When the button is clicked, actionPerformed is called
public void actionPerformed(ActionEvent e){
String textFieldText = inputField.getText(); // Get the text
showInput.setText(textFieldText); // Set the text on the JLabel
}
});
*/
你能说得更具体些吗?你到底想做什么?你的实际问题是什么?