In the last section we talked about the concept of GUI (graphical User Interface) and learned that GUI programming is mainly about two packages, namely: AWT and Swing, this section we will implement the first window through the classes and controls commonly used in swing programs.
Javax.swing.JFrame
JFrame is a Window object in the GUI that inherits from frame. The JFrame control is used to create a form in a swing program. As shown in table 1, the common construction method for JFrame.
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9E/26/wKiom1mMBHaA-IDcAABJy_nmhYc135.png "title=" Tu 1. PNG "alt=" Wkiom1mmbhaa-idcaabjy_nmhyc135.png "/>
Attention:
The Java language specifies that the default is invisible (that is, hidden) state when any window is instantiated in GUI programming, so it is not visible when we instantiate a JFrame object using a constructor method.
The JFrame also includes presentation forms and a number of methods for setting form properties such as size, color, and so on, as shown in table 2.
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9E/26/wKiom1mMBJGRchp3AAC9cov444A036.png "title=" Tu 2. PNG "alt=" Wkiom1mmbjgrchp3aac9cov444a036.png "/>
After understanding the common constructor and member methods of the JFrame class, we then use an example to create a simple window, as shown in Example 1.
"Example 1" creates a simple window
Package cn.sxt.views.testjframe;
Import Java.awt.Color;
Import Javax.swing.JFrame;
public class jframedemo1{
public static void Main (string[] args) {
JFrame f = new JFrame ();//Create a new form
F.setsize (500,400);//Set Form size
F.setvisible (TRUE);//Set form visible
F.setbackground (Color.Black);//Invalid Color setting
}
}
As above, the first line of code in the Main method instantiates a Window object, and the second line of code sets the size of the window (note: The dimension is positioned as a pixel) because the window defaults to invisible, so the third line of code is set to make the window visible. There is also a show () method in JFrame that makes the window visible, but is not recommended because show () can only make the window visible, but setvisible if the passed parameter is true the window is visible and the passed-in parameter is false and the window is not visible. So the setvisible method is more flexible than the show method.
The result of the execution of example 1:
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9E/26/wKiom1mMBNjyZabzAABRqrVJvLo113.png "title=" Tu 3. PNG "alt=" Wkiom1mmbnjyzabzaabrqrvjvlo113.png "/>
We also found that using the SetBackground method to change the color of the form is not valid in JFrame because the method is inherited from the frame. To be used in JFrame: Jframe.getcontentpane (). SetBackground (Color.bule); As shown in Example 2.
"Example 2" changes the color of the form
Package cn.sxt.views.testjframe;
Import Java.awt.Color;
Import Javax.swing.JFrame;
public class jframedemo2{
public static void Main (string[] args) {
JFrame f = new JFrame ();//Create a new form
F.setsize (500,400);//Set Form size
F.setvisible (TRUE);//Set form visible
F.setbackground (Color.Blue);//Invalid Color setting
To use JFrame to change the color of a form, you need to use this method
F.getcontentpane (). SetBackground (Color.Black);
}
}
Execution Result:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9E/26/wKiom1mMBZPgs6WeAABZP24yP70100.png "title=" Figure 3.png "alt=" Wkiom1mmbzpgs6weaabzp24yp70100.png "/>
The forms created in the above two examples are user-resized by default, and if we do not want the size of the window to be arbitrarily resized, and the code that creates the form is encapsulated, you can use the code in Example 3.
"Example 3" creates a non-resizable form
Package cn.sxt.views.testjframe;
Import Javax.swing.JFrame;
Class JFrameDemo3 extends JFrame {
Public JFrameDemo3 () {
Init ();
}
The method initializes the window.
private void init () {
Set the window title bar information
This.settitle ("first window");
Set window size to width: 500, Height: 400
This.setsize (500, 400);
Settings window cannot be resized
This.setresizable (FALSE);
The Settings window is visible
This.setvisible (TRUE);
}
}
public class Test {
//Call JFrameDemo3 's construction method to create and display the form
new JFrameDemo3 ();
}
execution result:
650) this.width=650; " Src= "Https://s3.51cto.com/wyfs02/M00/9E/26/wKiom1mMBfvxLG4lAABY6I9tOOg413.png" title= "TU 5.png" alt= " Wkiom1mmbfvxlg4laaby6i9toog413.png "/>
But careful readers will find that when the window is closed, the application does not end. This is because Java rules: the window default shutdown mode is only invisible, which causes us to close a window, the window is actually hidden, not the program is over, so to solve this problem, you need to this.setvisible (true) in the Init () method; This code is preceded by the addition of This.setdefaultcloseoperation (Jframe.exit_on_close); This method, the function of this method is to set the window's closing mode, its parameters are int, and JFrame has set a constant for a variety of different shutdown modes, jframe.exit_on_close means to exit the application when closed, Other common constants include the following: Jframe.dispose_on_close (which means to exit the window when closed), jframe.do_nothing_on_close (the constant means: do not do any processing when closed), etc.
Class JFrameDemo3 extends JFrame {
Public JFrameDemo3 () {
Init ();
}
The method initializes the window.
private void init () {
Set the window title bar information
This.settitle ("first window");
Set window size to width: 500, Height: 400
This.setsize (500, 400);
Settings window cannot be resized
This.setresizable (FALSE);
The program ends when the window is closed
This.setdefaultcloseoperation (Jframe.exit_on_close);
The Settings window is visible
This.setvisible (TRUE);
}
}
"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.
The notes contain six parts from shallow into deep:
A-java Introductory Phase
B-Database from beginner to proficient
C-hand Edge mobile front end and web front end
D-j2ee from understanding to combat
E-java Advanced Frame Fine Solution
F-linux and Hadoop
This article is from the "full stack Java Notes" blog, be sure to keep this source http://javanew.blog.51cto.com/12931675/1955146
14.2-Full Stack java notes: Java Swing Create window, original so simple!!!