2011-12-14 57 views
-3

我在NetBeans中有一个非常标准的Java桌面应用程序。我最近添加了一个jDialog模式作为欢迎横幅,并开始关闭应用程序的问题。如果我在主框架上使用默认菜单(文件>退出),它似乎总是正确关闭。如果我点击主窗口上的关闭按钮,框架将消失,但JVM仍将继续运行。Java程序不能用关闭按钮退出

删除调用以显示对话框完全解决了这个问题,所以我猜测这就是问题所在。我很确定这个对话框正在被正确地处理,因为这是我第一次猜测为什么JVM不会退出。

为了检查我的理智,不是调用我的欢迎对话框,而是开始调用NetBeans在创建应用程序时自动生成的默认showAboutBox()。同样的问题。当通过菜单调用对话框时工作正常(我不确定这是因为它是否是一个Action),但如果直接调用JVM则不会正确退出。

这两个对话框的setDefaultCloseOperation()设置为DISPOSE_ON_CLOSE

下面是调用AboutBox的(这是一样的我的欢迎对话框)的代码片段:

@Action 
public void showAboutBox() { 
    if (aboutBox == null) { 
     JFrame mainFrame = MyApp.getApplication().getMainFrame(); 
     aboutBox = new MyAboutBox(mainFrame); 
     aboutBox.setLocationRelativeTo(mainFrame); 
    } 
    MyApp.getApplication().show(aboutBox); 
} 

不管怎么说,我解决了这个问题我自己。

所以我想你不能调用构造函数对话框......

的修复...

/* 
* MyApp.java 
*/ 

import org.jdesktop.application.Application; 
import org.jdesktop.application.SingleFrameApplication; 

/** 
* The main class of the application. 
*/ 
public class MyApp extends SingleFrameApplication { 

    /** 
    * At startup create and show the main frame of the application. 
    */ 
    @Override protected void startup() { 
     show(new MyView(this)); 
     MyView.doThisCodeAfterTheMainFrameIsLoaded(); 
    } 

    /** 
    * A convenient static getter for the application instance. 
    * @return the instance of MyApp 
    */ 
    public static MyApp getApplication() { 
     return Application.getInstance(MyApp.class); 
    } 

    /** 
    * Main method launching the application. 
    */ 
    public static void main(String[] args) { 
     launch(MyApp.class, args); 
    } 

} 




/* 
* MyView.java 
*/ 

/** 
* The application's main frame. 
*/ 
public class MyView extends FrameView { 

    public MyView(SingleFrameApplication app) { 
     super(app); 

     initComponents(); 

     // Don't call a dialog here! 
     // showAboutBox(); 

    } 

    public static void doThisCodeAfterTheMainFrameIsLoaded() { 

     JFrame mainFrame = MyApp.getApplication().getMainFrame(); 
     JDialog welcome = new MyWelcome(mainFrame); 
     welcome.setLocationRelativeTo(mainFrame); 

     MyApp.getApplication().show(welcome); 

    } 

} 
+0

为了更好地帮助越早,张贴[SSCCE(http://sscce.org/)。顺便说一句 - 你的问题是什么? – 2011-12-14 16:34:50

+0

阅读,你能做到吗,先生? – Shawn 2011-12-14 17:32:38

回答

3

添加以下内容:

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

JFrame文档:

与框架不同,当用户尝试关闭窗口时,JFrame具有一些如何响应的概念。默认行为是当用户关闭窗口时简单地 隐藏JFrame。要更改默认的 行为,请调用setDefaultCloseOperation(int)方法。要制作 ,JFrame的行为与Frame实例相同,请使用 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)。

再看看JFrame.setDefaultCloseOperation