【发布时间】:2011-12-14 16:20:53
【问题描述】:
我在 NetBeans 中有一个非常标准的 Java 桌面应用程序。我最近添加了一个 jDialog 模式作为欢迎横幅,并开始遇到关闭应用程序的问题。如果我在主框架上使用默认菜单(文件 > 退出),它似乎总是正确关闭。如果我点击主窗口上的关闭按钮,框架将消失,但 JVM 仍将运行。
删除显示对话框的调用完全解决了问题,所以我猜这就是问题所在。我很确定对话框已正确处理,因为这是我第一次猜测 JVM 不会退出的两个原因。
为了检查我的理智,我没有调用我的欢迎对话框,而是开始调用默认的showAboutBox(),它是在创建应用程序时由 NetBeans 自动生成的。同样的问题。通过菜单调用该对话框时工作正常(我不确定这是否是因为它是一个动作)但如果直接调用会导致 JVM 无法正确退出。
两个对话框都将setDefaultCloseOperation() 设置为DISPOSE_ON_CLOSE。
下面是调用aboutBox的代码sn-p(和我的欢迎对话框一样):
@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);
}
}
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖 SSCCE。顺便说一句 - 你的问题是什么?
-
很高兴你把它整理好了。以后请不要使用脏话。