AWT窗口关闭侦听器/事件 [英] AWT Window Close Listener/Event
问题描述
如果这是一个n00b的问题,我很抱歉,但是,一旦我创建了Window侦听器,窗口事件和其他所有内容,我已经花费了太多的时间了,我该如何指定什么方法调用?这是我的代码:
I am sorry if this is a n00b question, but I have spent way too long for this once I create the Window listener, window event, and everything else, how do I specify what method to invoke? Here is my code:
private static void mw() {
Frame frm = new Frame("Hello Java");
WindowEvent we = new WindowEvent(frm, WindowEvent.WINDOW_CLOSED);
WindowListener wl = null;
wl.windowClosed(we);
frm.addWindowListener(wl);
TextField tf = new TextField(80);
frm.add(tf);
frm.pack();
frm.setVisible(true);
}
我正在尝试获取一个URL,并下载它,我已经做了一切工作,我只是想让窗口关闭。
I am trying to get a URL, and Download it, I have everything else worked out, I am just trying to get the window to close.
推荐答案
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FrameByeBye {
// The method we wish to call on exit.
public static void showDialog(Component c) {
JOptionPane.showMessageDialog(c, "Bye Bye!");
}
public static void main(String[] args) {
// creating/udpating Swing GUIs must be done on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame("Say Bye Bye!");
// Swing's default behavior for JFrames is to hide them.
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
showDialog(f);
System.exit(0);
}
} );
f.setSize(300,200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
另外查看 Runtime.addShutdownHook(Thread)
对于在关闭之前执行至关重要的任何操作。
Also look into Runtime.addShutdownHook(Thread)
for any action that is vital to perform before shutting down.
..产生不依赖于在系统上安装JVM / JRE以执行
..produce "Native" executables that do not depend on the JVM/JRE installed on the system to function"
更好的选择是:
- 使用Swing创建富客户端。
- 使用
deployJava.js
来检查用户具有适当的JRE。 - 使用 Java Web Start 安装并启动应用程序(并在需要时从网上更新)。
- Use Swing to create the rich client.
- Use
deployJava.js
to check the user has an appropriate JRE. - Use Java Web Start to install and launch the app. (and update it when needed) direct from the net.
<因为你提到小程序和下载请注意,只有受信任的小程序/应用程序JWS。可以跨域获取数据,它将采用相同的信任版本将文件保存到本地文件系统,也可以使用JWS&使用JNLP API文件服务。
And since you mention 'applet' and 'download' note that only a trusted applet/JWS app. can get data across domains, and it would take either a trusted version of same to save files to the local file system, or being deployed using JWS & using the JNLP API file services.
一旦你有一个富客户端应用程序。使用JWS启动 - 小程序可能变得无关紧要。
Once you have a rich client app. launched using JWS - the applet might become irrelevant.
OK - 我输入,这是代码的AWT版本。
OK - I give in. Here is an AWT version of that code.
import java.awt.*;
import java.awt.event.*;
class FrameByeBye {
// The method we wish to call on exit.
public static void showMessage() {
System.out.println("Bye Bye!");
}
public static void main(String[] args) {
Frame f = new Frame("Say Bye Bye!");
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
showMessage();
System.exit(0);
}
} );
f.setSize(300,200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
这篇关于AWT窗口关闭侦听器/事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!