第1个回答 2010-05-16
****************************************************************
三个类A.java, B.java和C.java的代码分别如下:
****************************************************************
//A.java
import javax.swing.JFrame;
public class A extends JFrame {
public A() {
this.setTitle("A");
this.setBounds(300, 300, 200, 100);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new A();
}
}
****************************************************************
//B.java
import javax.swing.JFrame;
public class B extends JFrame {
public B() {
this.setTitle("B");
this.setBounds(600, 300, 200, 100);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new B();
}
}
****************************************************************
//C.java
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.JPanel;
public class C extends JFrame implements ActionListener {
JPanel bPanel;
JButton a;
JButton b;
public C() {
a = new JButton("Show A");
b = new JButton("Show B");
a.addActionListener(this);
b.addActionListener(this);
bPanel = new JPanel(new FlowLayout());
bPanel.add(a);
bPanel.add(b);
this.setContentPane(bPanel);
this.setTitle("C");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("source: " + e.getSource());
String name = ((JButton) (e.getSource())).getText();
System.out.println("name: " + name);
if (name.equals("Show A")) {
new A();
} else if (name.equals("Show B")) {
new B();
}
}
public static void main(String[] args) {
new C();
}
}
****************************************************************本回答被提问者采纳