2009-12-22 61 views

回答

73

如果你想要在JFrame关闭您的应用程序终止,使用

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) 

,而不是

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

documentation

  • DO_NOTHING_ON_CLOSE(在WindowCons中定义tants):不要做任何事情;要求程序处理已注册WindowListener对象的windowClosing方法中的操作。
  • HIDE_ON_CLOSE(在WindowConstants中定义):在调用任何注册的WindowListener对象后自动隐藏框架。
  • DISPOSE_ON_CLOSE(在WindowConstants中定义):在调用任何已注册的WindowListener对象后,自动隐藏并处理该帧。
  • EXIT_ON_CLOSE(在JFrame中定义):使用系统退出方法退出应用程序。仅在应用程序中使用它。

这是我的回答澄清这个问题之前,仍然可能是有用的:

您可以在JFrame使用setVisible(false),如果你想再次显示相同的帧。
否则请致电dispose()remove all of the native screen resources

+0

谢谢,但我关闭窗口右上角的关闭按钮。它并不使用setVisible(false),而必须停止该线程。 – Keating 2009-12-22 06:32:56

+0

我想我必须重写一些主题,我不知道哪个主题,但我相信它不是关闭的主题。 – Keating 2009-12-22 06:34:50

+0

我不确定你试图达到什么目的,你能否编辑你的问题并添加一些关于你想要做什么以及哪些不起作用的信息? – 2009-12-22 06:39:54

3

它有帮助吗?

import java.awt.BorderLayout; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class TwoJFrames { 
    public static void main(String[] args) { 
     int nb = 4; 
     if (args != null && args.length > 0) { 
      nb = Integer.parseInt(args[0]); 
     } 

     final int frameCount = nb; 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       for (int i = 0; i < frameCount; i++) { 
        JFrame frame = new JFrame("Frame number " + i); 
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        JPanel p = new JPanel(new BorderLayout()); 
        p.add(new JLabel("Click on the corner to close..."), BorderLayout.CENTER); 
        frame.setContentPane(p); 
        frame.setSize(200, 200); 
        frame.setLocation(100 + 20 * i, 100 + 20 * i); 
        frame.setVisible(true); 
       } 
      } 
     }); 

    } 
} 
+1

这很有用,谢谢! – Keating 2009-12-22 07:32:33