我最近编了一个小程序
一共有两个JFame,其中frame1里面有JText和JBtton
而frame2中含有一个JText
我想通过frame1中的JButton按钮控制frame2中的JText显示fame11中JText中的内容
希望 高手给我演示一个程序
非常感谢
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
public class TwoFrame {
private JFrame jFrame1 = null; // @jve:decl-index=0:visual-constraint="94,45"
private JPanel jContentPane1 = null;
private JFrame jFrame2 = null; // @jve:decl-index=0:visual-constraint="444,50"
private JPanel jContentPane2 = null;
private JTextField jTextField2 = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
private boolean show=false;
/**
* This method initializes jFrame1
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame1() {
if (jFrame1 == null) {
jFrame1 = new JFrame("Frame1");
jFrame1.setSize(new Dimension(248, 158));
jFrame1.setContentPane(getJContentPane1());
}
return jFrame1;
}
/**
* This method initializes jContentPane1
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane1() {
if (jContentPane1 == null) {
jContentPane1 = new JPanel();
jContentPane1.setLayout(new FlowLayout());
jContentPane1.add(getJTextField1(), null);
jContentPane1.add(getJButton(), null);
}
return jContentPane1;
}
/**
* This method initializes jFrame2
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame2() {
if (jFrame2 == null) {
jFrame2 = new JFrame("Frame2");
jFrame2.setSize(new Dimension(272, 151));
jFrame2.setContentPane(getJContentPane2());
}
return jFrame2;
}
/**
* This method initializes jContentPane2
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane2() {
if (jContentPane2 == null) {
jContentPane2 = new JPanel();
jContentPane2.setLayout(new FlowLayout());
jContentPane2.add(getJTextField2(), null);
}
return jContentPane2;
}
/**
* This method initializes jTextField2
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setColumns(15);
}
return jTextField2;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setColumns(15);
}
return jTextField1;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton("显示");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(!show)
{
jTextField2.setText(jTextField1.getText());
show=true;
jButton.setText("清除");
}
else
{
jTextField2.setText("");
show=false;
jButton.setText("显示");
}
}
}
);
}
return jButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TwoFrame tf=new TwoFrame();
JFrame f1=tf.getJFrame1();
JFrame f2=tf.getJFrame2();
f1.setVisible(true);
f2.setVisible(true);
f1.setLocation(10,10);
f2.setLocation(200,200);
}
}