如何将java程序的两个模块合并到一个模块中?

我是java新手
现在刚写了两个(A.java B.java)文件,分别编译运行后成功运行了。
现在我想创建一个(C.java)做一个界面,上面有两个按钮,分别是A和B;
如果点A则能弹出A.java运行的内容,点B则能弹出B.java运行的内容;
请教高手该如何解决(可以的话请告诉我具体步骤:P)
如果有范例的话也可以发我邮箱[email protected]

第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();
}

}

****************************************************************本回答被提问者采纳
第2个回答  2010-05-16
ButtonA的click事件,new FormA();
ButtonB的click事件,new FormB();
第3个回答  2010-05-17
见一楼答案
相似回答