java-window Close

Source: Internet
Author: User

Reproduced:

Beginners often learn Java application often encounter the confusion of windows, such as the program:

 1 package applicationgraphicsinout; 
2 import java.awt.*;
3 Import java.awt.event.*;
4 public class Applicationgraphicsinout {
5 public static void Main (String args[])
6 {
7 New Frameinout ();
8}
9
Ten}
One class Frameinout extends Frame implements ActionListener
{
Label prompt;
TextField Input,output;
Frameinout ()
("Java application program for graphical interface") {
");
Prompt=new Label ("Please enter your name");
Input=new TextField (6);
Output=new TextField (20);
SetLayout (New FlowLayout ());
Add (Prompt);
Add (input);
Add (output);
Input.addactionlistener (this);
SetSize (300,200);
SetVisible (TRUE);
28}
public void actionperformed (ActionEvent e)
{
Output.settext (Input.gettext () + "Welcome to You");
32}

The program runs through Eclipse, but it can't be shut down!

What's the reason?

There are several workarounds for this:

1:

Change the main function to the following

1 public static void Main (String args[])
2 {
3 Frame fr=new frameinout ();
4 Fr.addwindowlistener (new Java.awt.event.WindowAdapter ()
5 {
6 Public void windowclosing (Java.awt.event.WindowEvent e)
7 {
8 system.exit (0);
9 }
(Ten });
11
-- }

I'm a little puzzled by this approach, Addwindowlistener () What's going on in parentheses, I'm a little puzzled, I haven't seen this form yet, but I guess it's possible to achieve java.awt.event.WindowAdapter () An abstract method within the class windowclosing (), but I have never seen this format for interpretation.

2:

In response to the Windows_closing event, each window has 3 control icons, in which the minimize and maximize action frame is automatically completed, and the action of closing the window cannot be achieved by clicking the Close icon, which requires the program to write the relevant code, in fact, this method is the same as the previous one. But in a different format, a beginner has a better understanding of the format.

Add a listener function inside the frameinout () constructor, and after adding the listener function, the constructor is as follows:

FrameInOut()    {        super("图形界面的Java Application程序");        prompt=newLabel("请输入您的名字");        input=newTextField(6);        output=newTextField(20);        setLayout(newFlowLayout());        addWindowListener(newHandleWin());  //添加监听函数,引发WindowEvent事件        add(prompt);        add(input);        add(output);        input.addActionListener(this);        setSize(300,200);        setVisible(true);    }

where Handlewin () for the inner class, the main implementation of WindowListener excuses, adding the listener will cause the WindowEvent class representative of the seven events, as follows:

(1) Window_activated: The representative window is activated (standby at the front of the screen).

(2) Window_deactivated: The window is deactivated (the original active window is deactivated after the other window is activated).

(3) Window_opened: The representative window is opened.

(4) Window_closed: The representative window is closed (occurs after the window is closed).

(5) Window_closing: The representative window is being closed (referred to before closing.) When you click the Close button on the window's title bar).

(6) Window_iconified: represents minimizing the window as an icon.

(7) Window_deiconified: Represents the restoration of the window from the icon

The main methods in the WindowEvent class are:

Public window GetWindow ();

This method returns the specific window object that raised the current WindowEvent event, and the same event reference returned by the GetSource () method.

Handlewin is defined as follows:

Class Handlewin extends Windowadapter
{
public void windowclosing (WindowEvent e)
{
(E.getwindow ()). Dispose ();
System.exit (0);
}
}

Handlewin is a subclass of the clipping class windowadapter for window events and overloads the Windowclosing () method.

Of course you can write that Handlewin

1 class Handlewin implements WindowListener
2 {
3 public void windowclosing (WindowEvent e)
4 {
5 (E.getwindow ()). Dispose ();
6 system.exit (0);
7}
8
9 @Override
Ten public void windowactivated (WindowEvent arg0) {
One//TODO auto-generated method stub
12
13}
14
@Override
+ public void windowclosed (WindowEvent arg0) {
/TODO auto-generated Method stub
18
19}
20
@Override
public void windowdeactivated (WindowEvent arg0) {
/TODO auto-generated Method stub
24
25}
26
@Override
-public void windowdeiconified (WindowEvent arg0) {
/TODO auto-generated Method stub
30
31}
32
@Override
public void windowiconified (WindowEvent arg0) {
//TODO auto-generated method stub
36
37}
38
@Override
-public void windowopened (WindowEvent arg0) {
$//TODO auto-generated method stub
42
43}
44}

It will implement all 7 abstract methods of WindowListener!

java-window Close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.