我已经建立好了两个窗口,都是用JFrame建立的。A窗口上有一个按钮JButton,按下按钮后,A窗口关闭,已有的B窗口打开,哪位大侠能告诉我怎么做啊??多谢了!!!

解决方案 »

  1.   

    import javax.swing.*;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public class Test{ private JFrame frame_a = null, frame_b = null; private JButton button_1 = null, button_2 = null; public Test() {
    frame_a = new JFrame("Frame A");
    frame_b = new JFrame("Frame B");
    button_1 = new JButton("new Frame A");
    button_2 = new JButton("new Frame B");
    button_1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    frame_a.dispose();
    frame_b.setVisible(true);
    }});
    button_2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    frame_b.dispose();
    frame_a.setVisible(true);
    }});
    JPanel pane_1 = new JPanel();
    JPanel pane_2 = new JPanel();
    pane_1.add(button_1);
    pane_2.add(button_2);
    frame_a.getContentPane().add(pane_1);
    frame_b.getContentPane().add(pane_2);
    frame_a.setSize(300, 200);
    frame_b.setSize(200, 100);
    frame_a.setVisible(true);
    } public static void main(String[] args) {
    new Test();
    }
    }
      

  2.   

    如果不想丢失frame_a的数据
    我觉得frame_a.dispose()这个地方改成setVisible(false)好点吧
    建议frame_a.dispose();和frame_b.setVisible(true);的顺序颠倒一下,窗口的切换会自然一点,对于一半配置的机器而言
      

  3.   

    窗口的名字可以在JFrame构造的时候创建new JFrame("Frame Title"); 也可以用setTitle("Frame Title");。 之后可用public String getTitle()来获取。
      

  4.   

    这种方法虽可实现,但不是很好,何不在按钮事件上new出一个JFrame,然后将主Frame进行隐藏而新Frame进行显示呢?