Create a form
When developing Java applications, you typically create windows with JFrame. Windows created with JFrame contain a title, Minimize button, maximize button, and Close button
When creating a window with JFrame, you need to set the action to be performed when you click the Close button , set the method to pass the SetDefault closeoperation (int operation) method of the JFrame object. The method's entry parameters can be selected from the static constants of the JFrame class, with optional static constants (below the code)
Public classTest () { Public Static voidMain (string[] args) {JFrame frame=NewJFrame ("New Window"); Create a new form, the title of the form is the content in "", JPanel panel=NewJPanel (); Jpanel is a panel container class, contained in swing; JTextArea TextArea=NewJTextArea (); JTextArea text area, JTextField text box panel.setlayout (NewGridLayout ()); layout is layouts; GridLayout Grid layout Textarea.settext ("Test"); //generate scroll bars when content in textarea is too longPanel.add (NewJScrollPane (TextArea)); Frame.add (panel); Add a panel to the newly created form; Frame.setsize (200,200); Set the size of the form frame.setvisible (true); Note that this step must be finally called, making this form visible, false is not seen;
Frame.setdefaultcloseoperation (frame. Exit_on_close); Defines the action of clicking the Close button;}}
setdefaultcloseoperation (int operation): sets the action that the user performs by default when "Close" is initiated on this form. The parameters in the method are explained as follows:
For "0" or Do_nothing_on_close:
(defined in windowconstants): do nothing; ask the program to handle the operation in the Windowclosing method of the registered WindowListener object.
For example, change the instance program code to F.setdefaultcloseoperation (f. do_nothing_on_close), or f.setdefaultcloseoperation (0), and then look at the effect, You can see that the window cannot be closed, the following is the same test method, no longer explained.
For "1" or hide_on_close
The form is automatically hidden after any registered WindowListener object is called. The program is not closed at this time, only the program interface is hidden. You can open Task Manager, you can see a process called "Java.exe" (if debugging runs multiple Java programs, you will see more than one "java.exe" process), if you use EditPlus test program, you will find that when you click the window's Close button to close the window, It is not possible to debug the program again because the program thread is not closed, and the Java.exe (if there are multiple "java.exe" processes, which are closed first, and then tested for the problem), will not be able to recompile the program until it is shut down in Task Manager.
For "2" or dispose_on_close
Automatically hides and releases the form after calling any registered WindowListener object. However, you continue to run the application, freeing up the resources used in the form.
For "3" exit_on_close (defined in JFrame): Exits the application using the System exit method. Used only in the application. End the application.
By default, this value is set to Hide_on_close.
When commenting out the F.setdefaultcloseoperation (F.exit_on_close) in the instance, the effect and the F.setdefaultcloseoperation (f. Hide_on_close) When the statement is played; or f.setdefaultcloseoperation (1);