怎么用java实现在同一界面点击两个按钮进入两个不同界面

希望能具体点,最好有代码,菜鸟伤不起
2024-11-28 20:58:15
推荐回答(2个)
回答1:

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

回答2:

设计两个JPanel 或JFrame 点不同按钮打开不同界面不行吗?