使用监听事件,有两个界面A和B,当单击A中的一个按扭时,弹出界面B,切界面A不见,那个监听事件怎么写?

解决方案 »

  1.   

    package com.ddpie.frame;import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class TestFrameSwitch {
    public static void main(String[] args) {
    final JFrame frameA = new JFrame("窗口A");
    final JFrame frameB = new JFrame("窗口B");
    JButton buttonA = new JButton("切换成窗口B");
    JButton buttonB = new JButton("切换成窗口A");
    frameA.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
    frameB.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
    frameA.getContentPane().add(buttonA);
    frameB.getContentPane().add(buttonB);
    frameA.setSize(300, 200);
    frameB.setSize(600, 600);
    frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frameB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frameA.setVisible(true);
    buttonA.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    frameA.setVisible(false);
    frameB.setVisible(true);
    }
    });
    buttonB.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    frameB.setVisible(false);
    frameA.setVisible(true);
    }
    });
    }}
      

  2.   

    如果是两个panel切换可以考虑用CardLayout
      

  3.   

    对于这种情况 建议写两个单独的Frame类 不要写在一个类中 添加功能时会很乱 也难后期修改
      

  4.   

    一般来说,一个Frame就应该是一个类