《Java 编程技巧1001条》 第422条: 如何退出应用程序

xiaoxiao2021-02-28  25

《Java 编程技巧1001条》 第11章 事件驱动  第422条 如何退出应用程序

422 Making Sure You Can Quit Your Applications

422 确信你可以退出应用程序

Most operating systems wrap your Java application inside a standard window frame. Within these frames, a menu or icon usually lets the user close a window (ending the program). When you select this item, the operating system generates a WINDOW_DESTROY message. Unless your program handles the WINDOW_DESTROY message, your application will not quit. In fact, if your application is simple, you might not have any way to quit your application. If you are using UNIX, you could kill the process, but that is a little drastic. Instead, your program should always handle the WINDOW_DESTROY event. Your application can then quit easily and naturally. The following program, quiteApplication.java, shows how to handle a WINDOW_DESTROY event:

大多数操作系统把你的应用程序限制在一标准的窗口帧中.在这些帧中,通常都会设置一项菜单或一个图标使用户能关闭这个闯口(结束程序). 当你选择这一项时,操作系统产生一个 WINDOW_DESTROY(窗口消失)消息. 除非你的程序处理WINDOW_DESTROY消息,你的程序将不会退出. 事实上,如果你的应用程序是简单的, 你可能没有任何办法退出应用程序. 如果你使用UNIX,你可以kill这个进程,但这是有点太激列了. 可取的办法是你的程序永远处理WINDOW_DESTROY使件, 这时你的应用程序就可方便地容易地退出. 以下的程序quiteApplication.java向你说明怎样处理 WINDOW_DESTROY事件:

import java.awt.*;

class quitApplication extends Frame {    public quitApplication(String label)     {       setTitle(label);       resize(100,100);     }    public boolean handleEvent(Event evt)     {       if (evt.id == Event.WINDOW_DESTROY)         System.exit(0);               // Quit the application       return(super.handleEvent(evt)); // Dont handle other events     }      public static void main(String[] args)     {       testApplication f = new testApplication("Window quit test");       f.show();     } } 423 Detecting When a Window Is Iconified 423 检测窗口何时已图标化(Iconified) You have learned how to catch many events. Another pair of events that a  Java application receives are WINDOW_ICONIFY and WINDOW_DEICONIFY. You  should note that icon events are platform dependent because not all  operating systems iconify applications. You may find it useful to handle  icon events if you wish to disable a graphics intensive program from  updating while it is iconified. There usually is no reason to update a  window that is not visible. The following code demonstrates how to disable  the paint method if an application is iconified: 你已知道了怎样去俘获许多种的事件. Java程序接受的另一对事件是WINDOW_ICONIFY (窗口图标化)和WINDOW_DEICONIFY(窗口去图标化). 你必须注意icon事件是和平台 有关的,因为并不是所有的操作系统都把应用程序图标化. 你可发现, 如果你希望禁 止一个已图标化的图形密集程序刷新, 利用icon事件是有用的. 通常没有理由要刷新 一个不可见窗口. 以下的代码说明了一个应用程序已图标化时怎样禁止paint方法: import java.awt.*;    class iconifyApplication extends Frame {    boolean icon = false;       public iconifyApplication(String label)      {        setTitle(label);        resize(100,100);      }       public boolean handleEvent(Event evt)      {        System.out.println("here");        if (evt.id == Event.WINDOW_DESTROY)          System.exit(0); // quit application        else if (evt.id == Event.WINDOW_ICONIFY)          icon = true;        else if (evt.id == Event.WINDOW_DEICONIFY)          icon = false;        return(super.handleEvent(evt)); // Dont handle other events      }         public void paint(Graphics g)      {        if (!icon)           System.out.println("update");              else          System.out.println("don't update");      }         public static void main(String[] args)      {        iconifyApplication f = new iconifyApplication ("iconify");        f.show();      }  } 1001 Java Programming Tips Page  PAGE 1652  of  numpages  15 filename  jt074_095.doc
转载请注明原文地址: https://www.6miu.com/read-2400121.html

最新回复(0)