import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonJFrame extends JFrame {
ButtonJFrame() {
super("ButtonJFrame");
JButton btnA = new JButton("打开FrameA");
JButton btnB = new JButton("打开FrameB");
this.setLayout(new BorderLayout());
this.getContentPane().add(btnA, BorderLayout.WEST);
this.getContentPane().add(btnB, BorderLayout.EAST);
btnA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FrameA().setVisible(true);
}
});
btnB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FrameB().setVisible(true);
}
});
this.setSize(320, 240);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new ButtonJFrame();
}
class FrameA extends JFrame {
FrameA() {
super("FrameA");
this.setSize(160, 120);
}
}
class FrameB extends JFrame {
FrameB() {
super("FrameB");
this.setSize(160, 120);
}
}
}
设计两个JPanel 或JFrame 点不同按钮打开不同界面不行吗?