Java 事件,但不关闭窗口
我想在关闭主应用程序窗口时显示“确认关闭”窗口,但不会使其消失。现在我正在使用一个Java 事件,但不关闭窗口,java,events,user-interface,Java,Events,User Interface,我想在关闭主应用程序窗口时显示“确认关闭”窗口,但不会使其消失。现在我正在使用一个windowsListener,更具体地说是windowsClosing事件,但是当使用此事件时,主窗口将关闭,我希望它保持打开状态 以下是我正在使用的代码: 注册侦听器 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { thisW
windowsListener
,更具体地说是windowsClosing
事件,但是当使用此事件时,主窗口将关闭,我希望它保持打开状态
以下是我正在使用的代码:
注册侦听器
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
thisWindowClosing(evt);
}
});
处理事件的实施:
private void thisWindowClosing(WindowEvent evt) {
new closeWindow(this);
}
此外,我还尝试在thisWindowClosing()
方法中使用this.setVisible(true)
,但它不起作用
有什么建议吗?你能发布到目前为止的代码吗?+1对于一个常见的场景,我从来没有想过如何实现tanks来获得答案,但这不起作用。单击“取消”后,主窗口将关闭。我不知道该说什么。它在Java5和Java6下非常适合我。请确保粘贴并编译我的示例程序,以证明我的代码正常工作。只有到那时,您才应该开始将我的方法转移到您现有的代码中。您是否记得调用frame.setDefaultCloseOperation(JFrame.DO\u NOTHING\u ON\u CLOSE);在你的镜框上?没错…这就是问题所在。如果我对我的代码调用“this.setDefaultCloseOperation(JFrame.DO\u NOTHING\u ON\u CLOSE)”,它工作得非常好。你真是太棒了!
package org.apache.people.mclark.examples;
import java.awt.event.*;
import javax.swing.*;
public class ClosingFrame extends JFrame {
public ClosingFrame() {
final JFrame frame = this;
// Setting DO_NOTHING_ON_CLOSE is important, don't forget!
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int response = JOptionPane.showConfirmDialog(frame,
"Really Exit?", "Confirm Exit",
JOptionPane.OK_CANCEL_OPTION);
if (response == JOptionPane.OK_OPTION) {
frame.dispose(); // close the window
} else {
// else let the window stay open
}
}
});
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClosingFrame().setVisible(true);
}
});
}
}