我想点击按键后关闭这个界面,该怎么做

解决方案 »

  1.   

    在事件里,把界面的visible属性设置为false
      

  2.   

    System.exit(0);
    这个方法可以
      

  3.   

    这个方法是关闭整个程序;设置visible为false是把窗口关闭,但不关闭程序。
      

  4.   

    哦  LZ不好意思  没看明白问题  我以为楼主要关闭整个界面呢  
    那就setvisiable(false)吧
      

  5.   

    dispose()才是关闭
    setVisible(false);只是隐藏
      

  6.   

    看了下API  
    Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be ed as undisplayable. 
    The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed 
    还是dispose()好  以前没注意就一直用的setVisible(false);
    虽然视觉效果是一样的 但没有释放资源 不太好
      

  7.   

    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(-1);
    }
      

  8.   


    正解!如果说使用了setVisible(),那么你就仅仅是把界面给隐藏了,但是依旧使用你的CPU
    当你使用的是dispose()时,它会释放内存。两者的差距还是很大的 
      

  9.   

    System.exit(0);/**正常退出*/
    System.exit(-1);/**非正常退出*/
    setVisible()在frame当中也有出现过
      

  10.   

    楼主要的是哪种关闭方式呢?import java.awt.Frame;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class T { public static void main(String[] args) {
    final Frame frame1 = new Frame("1");
    final Frame frame2 = new Frame("2");
    Frame frame3 = new Frame("3");
    frame1.setBounds(0, 0, 200, 200);
    frame2.setBounds(200, 0, 200, 200);
    frame3.setBounds(400, 0, 200, 200);
    frame1.setVisible(true);
    frame2.setVisible(true);
    frame3.setVisible(true);
    frame1.addWindowListener(new WindowAdapter() {
    @Override
    // 关闭当前窗口。
    public void windowClosing(WindowEvent e) {
    frame1.dispose();
    }
    });
    frame2.addWindowListener(new WindowAdapter() {
    @Override
    // 隐藏当前窗口。
    public void windowClosing(WindowEvent e) {
    frame2.setVisible(false);
    }
    });
    frame3.addWindowListener(new WindowAdapter() {
    @Override
    // 系统退出。
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    }}