The first step in creating a GUI program is to create a container class to accommodate other components, a common window being a container. Containers are also components that organize, manage, and display other components.
1. Overview of Top Containers
Top-level containers are the basis for graphic programming, and everything graphical must be included in the top-level containers. The top container is the main window involved in any GUI program and is the container component that displays and hosts components.
There are three top-level containers available in Swing, JFrame, JDialog, and JApplet.
-
JFrame: A class for frame windows with icons for borders, captions, closures, and minimizing windows. Applications with GUI use at least one frame window. [Key]
-
JDialog: A class for dialogs. [Understanding]
-
JApplet: A Java Applet class for using Swing components. [Not involved]
2. An overview of JFrame classes
JFrame: A window that usually contains a title, icon, action button (close, minimize, maximize), menu bar, toolbar, and so on. Multiple windows can be created in a process and can be displayed, hidden, or destroyed when appropriate.
-
JFrame is a top-level frame class, like a window frame. It is also a container class. This frame can be embedded in several glass windows.
Common methods in JFrame classes
-
JFrame() creates a form object (construction method)
-
void setSize(int width,int height) Set Form Size
-
void setVisible(true/false) shows or hides components
-
void setTitle(String title) Sets the title of the form
-
void setDefaultCloseOperation(int operation) Sets the default action after the window closes button is clicked
-
reference value
-
JFrame.DO_NOTHING_ON_CLOSE constant value: 0, no action performed
-
JFrame.HIDE_ON_CLOSE constant value: 1, hide window (process will not end). Calling setVisible(true) again will show up again.
-
JFrame.DISPOSE_ON_CLOSE constant value: 2, destroy windows, if all displayable windows are DISPOSE, the process may end automatically.
-
JFrame.EXIT_ON_CLOSE constant value: 3, exit process
-
-
-
Void setLocation RelativeTo (null) settings window centered display
3. Create a form using the JFrame class
3.1 Create forms directly in the main program entry
package com.test; import javax.swing.JFrame; public class Test { public static void main(String[] args) { // Instantiate a JFrame Form JFrame jFrame = new JFrame(); jFrame.setTitle("Title");// Set Form Title jFrame.setSize(300, 300);// Set Form Size jFrame.setDefaultCloseOperation(3);// Set Form Close Mode jFrame.setLocationRelativeTo(null);// Set Form Centering jFrame.setVisible(true);// Set Form Visibility } }
[Note] Placing code that implements the form directly in the main program entry is not easy for later expansion and maintenance, but using custom classes to implement the window can solve these problems.
3.2 Custom Implementation Create Form
Technological process:
-
Customize a class inheritance JFrame in the package specified by the Java project
-
Writing parameterless constructors for a custom class
-
Calling a method to set related properties within a constructor
-
Instantiate the object of the custom class in the test class to display the form
package com.zking.ui; import javax.swing.JFrame; //1. Customize a class inheritance JFrame in the package specified by the Java project public class HelloWorld extends JFrame { // 2. Write parameterless constructors for custom classes public HelloWorld() { // 3. Call methods to set related properties within constructors this.setTitle("First JFrame forms");// Set the title of the form this.setSize(300, 300);// Set the width and height of the form this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Set the form's default close mode this.setLocationRelativeTo(null);// Set the form to center on the screen this.setVisible(true);// Set Form Visibility } }
package com.zking.ui; public class Test { public static void main(String[] args) { // 4. Instantiate the object of the custom class in the test class to display the form HelloWorld hw = new HelloWorld(); } }
Design sketch: