AWT窗口关闭监听器/事件 [英] AWT Window Close Listener/Event

查看:414
本文介绍了AWT窗口关闭监听器/事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,如果这是一个的n00b问题,但我已经花了的办法的太长,这一次,我创建的窗口侦听器,窗口事件,以及其他一切,我怎么指定什么方法调用?这里是我的code:

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);

}

我想获得一个网址,并下载它,我拥有一切制定出来的,我只是试图让窗口关闭。

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);

            }
        });
    }
}


还应考虑<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#addShutdownHook-java.lang.Thread-\"><$c$c>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"

一个更好的选择是:


  1. 使用摆动创建丰富的客户端。

  2. 使用 deployJava.js 来检查用户有一个合适的JRE。

  3. 使用 Java Web Start的的安装和启动应用程序。 (并在需要时更新)直接从网络。

  1. Use Swing to create the rich client.
  2. Use deployJava.js to check the user has an appropriate JRE.
  3. 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.

确定 - 我给在这里是code的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆