Java NetBeans平台在关闭时不执行任何操作
我正在构建一个NetBeans平台应用程序。当用户单击主窗口的X时,我希望应用程序什么也不做,并显示密码JDialog。如果密码正确,请关闭应用程序,否则不要关闭应用程序。我该怎么做?我已经创建了一个侦听器类,它将显示密码JDialog,但是如何阻止应用程序关闭呢?类似于JFrame的setDefaultCloseOperation,并将其设置为在关闭时不执行任何操作Java NetBeans平台在关闭时不执行任何操作,java,netbeans-7,netbeans-platform,Java,Netbeans 7,Netbeans Platform,我正在构建一个NetBeans平台应用程序。当用户单击主窗口的X时,我希望应用程序什么也不做,并显示密码JDialog。如果密码正确,请关闭应用程序,否则不要关闭应用程序。我该怎么做?我已经创建了一个侦听器类,它将显示密码JDialog,但是如何阻止应用程序关闭呢?类似于JFrame的setDefaultCloseOperation,并将其设置为在关闭时不执行任何操作 public class Listener extends WindowAdapter { priva
public class Listener extends WindowAdapter {
private Frame frame;
@Override
public void windowActivated(WindowEvent event) {
frame = WindowManager.getDefault().getMainWindow();
frame.setSize(946, 768);
}
@Override
public void windowClosing(WindowEvent event) {
ShutDownMainWindowJDialog shutDownMainWindowJDialog;
shutDownMainWindowJDialog = new ShutDownMainWindowJDialog(null, true);
shutDownMainWindowJDialog.exeShutDownMainWindowJDialog();
shutDownMainWindowJDialog.setLocationRelativeTo(frame);
shutDownMainWindowJDialog.setVisible(true);
}
}
public class Installer extends ModuleInstall {
@Override
public void restored() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Frame frame = WindowManager.getDefault().getMainWindow();
frame.addWindowListener(new Listener());
}
});
}
}
在您的
windowClosing()
中,从用户处获取密码,并dispose()
帧(如果正确)
public class PasswordToClose extends WindowAdapter {
private JFrame frame;
public PasswordToClose(JFrame frame) {
this.frame = frame;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Password to close");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new PasswordToClose(frame));
frame.setSize(200, 200);
frame.setVisible(true);
}
@Override
public void windowClosing(WindowEvent evt) {
String password = JOptionPane.showInputDialog(frame, "Enter password");
if ("secret".equals(password)) {
frame.dispose();
}
}
}
这很容易。所以,使用安装程序创建模块并覆盖关闭方法(可能是),然后显示对话框并返回false(不关闭应用程序)。吉尔卡 这就是结论: 添加到模块安装程序/激活器
package master;
import org.openide.modules.ModuleInstall;
/**
* Manages a module's lifecycle. Remember that an installer is optional and
* often not needed at all.
*/
public class Installer extends ModuleInstall {
@Override
public void close() {
//your module shutdown
}
@Override
public boolean closing() {
// this is place for your Dialog() and return:
//
// true if you want to enable close the app
// other return false
/*
if you force shutdown app try LifecycleManager.getDefault().exit();
System.exit(0) is very dummy, because it does not respect betweenmodule dependencyis
}
}
吉尔卡是怎么做到的?JFrame someJFrame=WindowManager.getDefault().getMainWindow();给我一个错误创建一个新的JFrame?哈哈<代码>JFrame=新JFrame()但如果您承诺只使用
框架
,我下面的答案就行了。您可以拥有一个扩展JFrame
的类,并将其用于主窗口。@jadrijan这个新代码有效吗?应该这样做。如果你看到错误,那么你应该发布一个编辑,显示你收到的错误。嗨,乔纳森。我通过重写ModuleInstall的closing()方法使其工作。这一切都相当简单,但当一个人不熟悉一个框架时,这一切都很困难。感谢您的帮助,因为他通常声称他不能使用JFrame,我已经建议他只使用JFrame及其setDefaultClose,但他不喜欢。您好,我认为,考虑WindowClose并不好,因为NB是基于模块系统的框架。Yust 1模块用于“关闭操作”;-)简单的使用过盈关闭或关闭。JirkaI在一个多月前就知道了,但无论如何谢谢你的回答,我真的很感谢你的帮助。