Objective
Swing is a development toolkit for developing the Java application user interface. It is based on the abstract Window Kit (AWT) to enable Cross-platform applications to use any pluggable styling. Swing developers can use the rich, flexible features and modular components of swing to create elegant user interfaces with little code.
Swing is a GUI toolkit designed for Java.
Swing is part of the Java base class.
Swing includes a graphical user interface (GUI) device such as a text box, a button, a separator pane, and a table.
Swing offers many better screen-display elements than AWT. They are written in pure Java, so they can be run across platforms like Java itself, unlike AWT. They are part of the JFC. They support replaceable panels and themes (a variety of operating system default-specific themes), but instead of actually using the devices provided by the native platform, they simply mimic them on the surface. This means that you can use any of the Java-supported panels on any platform. The disadvantage of lightweight components is that it is slower to perform, with the advantage that uniform behavior can be applied across all platforms.
Hello World Program
The Helloworldswing.java file code is as follows:
Import javax.swing.*;
The public class Helloworldswing {
/**{
* Creates and displays the GUI. For thread-safe consideration,
This method is invoked in the event call thread.
* *
private static void Createandshowgui () {
//ensures a beautiful appearance style
jframe.setdefaultlookandfeeldecorated ( true);
Create and set Windows
JFrame frame = new JFrame ("helloworldswing");
Frame.setdefaultcloseoperation (jframe.exit_on_close);
Add the "Hello World" tab
JLabel label = new JLabel ("Hello World");
Frame.getcontentpane (). Add (label);
Display window
frame.pack ();
Frame.setvisible (true);
public static void Main (string[] args) {
//Display application GUI
javax.swing.SwingUtilities.invokeLater (New Runnable () c22/>public void Run () {
Createandshowgui ();}}
);
}
Perform the following command output results:
$ javac Helloworldswing.java
$ java helloworldswing
A User login Box instance
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 = NE
W JFrame ("Login Example");
Setting the width and height of frame frame.setsize (350, 200);
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
/* Create panel, this HTML-like div tag * We can create multiple panels and specify positions in the JFrame * panel we can add text fields, buttons and other components.
* * JPanel panel = new JPanel ();
Add Panels Frame.add (panel);
* * Invoke user-defined method and add component to Panel/placecomponents (panel);
Set the interface visible frame.setvisible (TRUE); } private static void Placecomponents (JPanel panel) {/* Layout section We don't do a lot of introductions here. * Set layout to NULL */Panel.setla
Yout (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 the new size is specified by width and height.
* * Userlabel.setbounds (10,20,80,25);
Panel.add (Userlabel);
* * Create text fields for user input * * JTextField Usertext = new JTextField (20);
Usertext.setbounds (100,20,165,25);
Panel.add (Usertext);
Enter the text field for the password JLabel passwordlabel = new JLabel ("Password:");
Passwordlabel.setbounds (10,50,80,25);
Panel.add (Passwordlabel);
* * This is similar to the text field for input * But the information entered is replaced with a dot number for the security containing 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); }
}
Perform the following command output results:
$ javac Swingloginexample.java
$ java swingloginexample
Conceptual Analysis:
The basic idea of JFrame –java GUI program is based on JFrame, which is the object of window on screen, which can be maximized, minimized and closed.
The Panel container class in the JPanel –java Graphical user interface (GUI) toolkit swing, contained in the javax.swing package, can be nested and is a lightweight container that combines components with the same logical functionality in a form. You can add to the JFrame form ...
JLabel –jlabel objects can display text, images, or both. You can specify where the label contents are aligned in the label display area by setting vertical and horizontal alignment. By default, labels are centered vertically in their display area. By default, labels that display only text are aligned at the start edge, while labels that display only the image are centered horizontally.
JTextField – A lightweight component that allows you to edit single-line text.
JPasswordField – allows us to enter a line of words like the input box, but hide the asterisk (*) or point create password (password)
JButton An instance of the –jbutton class. Use to create a "Login" in a similar instance of a button.
Well, this article will come to an ending. Here is the basic knowledge, with this foundation, the design of a complex graphical user interface is not difficult!