新打开的textfield上的内容可以传回到老的frame上的textfieldimport javax.swing.*;import java.awt.event.*; import java.awt.BorderLayout;public class ArgPass extends JFrame { private static JFrame jfTarget = new JFrame(); private JTextField jtfTarget = new JTextField("target",10); private JTextField jtfSource = new JTextField("source",10); public ArgPass(){ add(jtfSource);//add text field to the source frame;
JButton jb = new JButton("open new panel");//add button to source frame; add(jb,BorderLayout.SOUTH); jb.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) { jfTarget.setVisible(true);//open the target JFrame while the button pressed;
} });
//new JFrame property; jfTarget.add(jtfTarget);// add a text field to new frame; jtfTarget.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) { jtfSource.setText(jtfTarget.getText());//source text field show the same message with the one on target; //jtfSource.requestFocusInWindow();
import java.awt.BorderLayout;public class ArgPass extends JFrame {
private static JFrame jfTarget = new JFrame();
private JTextField jtfTarget = new JTextField("target",10);
private JTextField jtfSource = new JTextField("source",10);
public ArgPass(){
add(jtfSource);//add text field to the source frame;
JButton jb = new JButton("open new panel");//add button to source frame;
add(jb,BorderLayout.SOUTH);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jfTarget.setVisible(true);//open the target JFrame while the button pressed;
}
});
//new JFrame property;
jfTarget.add(jtfTarget);// add a text field to new frame;
jtfTarget.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtfSource.setText(jtfTarget.getText());//source text field show the same message with the one on target;
//jtfSource.requestFocusInWindow();
}
});
jfTarget.setTitle("target");
jfTarget.setSize(200, 200);
jfTarget.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfTarget.setLocation(410,200);
}
public static void main(String[] args){
ArgPass frame = new ArgPass();
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("source frame");
frame.setLocation(200,200);
frame.setVisible(true);
}}