日期:2014-05-20 浏览次数:20749 次
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Frame1 {
public static void main(String []args){
JButton button1 = new JButton("button1");
JFrame frame1 = new JFrame("frame1");
button1.addActionListener(new Listener1(button1, frame1));
frame1.add(button1);
frame1.setLayout(new FlowLayout());
frame1.setBounds(400, 100, 400, 300);
frame1.setVisible(true);
}
}
class Frame2{
public Frame2(JButton button1, JFrame frame1){
JTextField tf = new JTextField(20);
JButton button2 = new JButton("修改");
button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
JFrame frame2 = new JFrame("frame2");
frame2.setLayout(new FlowLayout());
frame2.setBounds(450, 150, 300, 200);
frame2.add(tf);
frame2.add(button2);
frame2.setVisible(true);
}
}
class Listener1 implements ActionListener{
JButton button;
JFrame frame;
public Listener1(JButton button, JFrame frame){
this.frame = frame;
this.button = button;
}
public void actionPerformed(ActionEvent e){
Frame2 frame2 = new Frame2(button,frame);
}
}
class Listener2 implements ActionListener{
String taskNameOld, taskNameNew;
JButton button;
JFrame frame;
public Listener2(JButton button, String taskNameNew, JFrame frame){
this.frame = frame;
this.taskNameOld = button.getText();
this.taskNameNew = taskNameNew;
}
public void actionPerformed(ActionEvent e){
if(taskNameNew != taskNameOld)
{
button.setText(taskNameNew);//运行时Eclipse报错
frame.repaint();
}
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Frame1 {
static JButton button1;
public static void main(String []args){
button1 = new JButton("button1");
JFrame frame1 = new JFrame("frame1");
button1.addActionListener(new Listener1(button1, frame1));
frame1.add(button1);
frame1.setLayout(new FlowLayout());
frame1.setBounds(400, 100, 400, 300);
frame1.setVisible(true);
}
static JButton getbun(){
return button1;