Preface
Swing is a development toolkit for developing the user interface of Java applications. It is based on the Abstract Window Toolkit (AWT) to enable cross-platform applications to use any pluggable appearance style. Swing developers can use Swing’s rich, flexible features and modular components to create an elegant user interface with very little code.
Swing is a GUI toolkit designed for Java.
Swing is part of the JAVA basic class.
Swing includes graphical user interface (GUI) devices such as text boxes, buttons, separating panes and tables.
Swing provides many better screen display elements than AWT. They are written in pure Java, so they can run across platforms just like Java itself, which is not like AWT. They are part of JFC. They support replaceable panels and themes (specific themes for various operating systems defaults), but instead of really using devices provided by native platforms, they simply mimic them on the surface. This means you can use any panel supported by JAVA on any platform. The disadvantage of lightweight components is that they are slow to execute, and the advantage is that they can adopt unified behavior on all platforms.
Hello World Programs
HelloWorldSwing.java file code is as follows:
import javax.swing.*;public class HelloWorldSwing { /**{ * Create and display the GUI. For thread safety reasons, * is called in the event calling thread. */ private static void createAndShowGUI() { // Ensure a beautiful appearance style JFrame.setDefaultLookAndFeelDecorated(true); // Create and set window JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add "Hello World" tag JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); // Show window frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // Display application GUI javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }} Execute the following command to output the result:
$ javac HelloWorldSwing.java$ java HelloWorldSwing
An instance of a user login box
The SwingLoginExample.java file code is as follows:
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField; public class SwingLoginExample { public static void main(String[] args) { // Create JFrame instance JFrame frame = new JFrame("Login Example"); // Setting the width and height of frame frame.setSize(350, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* Create a panel, this HTML-like div tag* We can create multiple panels and specify locations in JFrame* In the panel we can add text fields, buttons and other components. */ JPanel panel = new JPanel(); // Add panel frame.add(panel); /* * Call user-defined methods and add components to the panel */ placeComponents(panel); // Set the interface to visible frame.setVisible(true); } private static void placeComponents(JPanel panel) { /* We will not introduce the layout part here* Set the layout to null */ panel.setLayout(null); // Create JLabel JLabel userLabel = new JLabel("User:"); /* This method defines the location of the component. * setBounds(x, y, width, height) * x and y specify the new position in the upper left corner, and width and height specify the new size. */ userLabel.setBounds(10,20,80,25); panel.add(userLabel); /* * Create text field for user input*/ JTextField userText = new JTextField(20); userText.setBounds(100,20,165,25); panel.add(userText); // Enter the text field for password JLabel passwordLabel = new JLabel("Password:"); passwordLabel.setBounds(10,50,80,25); panel.add(passwordLabel); /* *This text field similar to input* However, the input information will be replaced by dots, which is used to contain the security of the password*/ JPasswordField passwordText = new JPasswordField(20); passwordText.setBounds(100,50,165,25); panel.add(passwordText); // Create login button JButton loginButton = new JButton("login"); loginButton.setBounds(10, 80, 80, 25); panel.add(loginButton); }}Execute the following command to output the result:
$ javac SwingLoginExample.java$ java SwingLoginExample
Concept analysis:
The basic idea of JFrame java's GUI program is based on JFrame, which is an object of window on the screen, which can be maximized, minimized and closed.
The panel container class in the JPanel Java graphical user interface (GUI) toolkit swing is included in the javax.swing package and can be nested. Its function is to combine components with the same logical functions in the form. It is a lightweight container that can be added to the JFrame form. .
JLabel A JLabel object can display text, images, or both. You can specify where the tag contents are aligned in the tag display area by setting vertical and horizontal alignment. By default, labels are vertically centered within their display area. By default, labels that display only text are aligned in the beginning edges; labels that display only images are aligned in the center horizontally.
JTextField is a lightweight component that allows editing of single-line text.
JPasswordField allows us to enter a line of words like an input box, but hide the asterisk (*) or dot create a password (password)
JButton An instance of the JButton class. Used to create buttons similar to "Login" in instances.
Okay, this article has come to an end. All we talk about here is basic knowledge. With this foundation, it is not difficult to design a more complex graphical user interface!