Introduction to Java Swing
Category Programming Techniques
Swing is a GUI toolkit designed for Java.
Swing is part of the Java Foundation Classes.
Swing includes GUI components such as text fields, buttons, split panes, and tables.
Swing offers many screen display elements that are better than AWT. They are written in pure Java, so they can run across platforms just like Java itself, unlike AWT. They are part of the JFC. They support replaceable panels and themes (the default themes unique to various operating systems), but they do not really use the devices provided by the native platform, but only imitate them superficially. This means you can use any panel supported by Java on any platform. The downside of lightweight components is slower execution speed, but the advantage is that they can have uniform behavior on all platforms.
Hello World Program
The code for the HelloWorldSwing.java file is as follows:
import javax.swing.*;
public class HelloWorldSwing {
/**{
* Create and display the GUI. For thread safety,
* this method is called in the event dispatching thread.
*/
private static void createAndShowGUI() {
// Ensure a nice look and feel
JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a "Hello World" label
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
// Display the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Display the application GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Execute the following commands for the output:
$ javac HelloWorldSwing.java
$ java HelloWorldSwing
An Example of a User Login Box
The code for the SwingLoginExample.java file 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 a JFrame instance
JFrame frame = new JFrame("Login Example");
// Set the width and height of the frame
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create a panel, similar to the div tag in HTML
* We can create multiple panels and specify their positions
* in the JFrame, and we can add text fields, buttons, and other components to the panels.
*/
JPanel panel = new JPanel();
// Add the panel
frame.add(panel);
/*
* Call the user-defined method and add components to the panel
*/
placeComponents(panel);
// Set the interface to be visible
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
/* We won't go into the layout part here
* Here we set the layout to null
*/
panel.setLayout(null);
// Create a JLabel
JLabel userLabel = new JLabel("User:");
/* This method defines the position of the component.
* setBounds(x, y, width, height)
* x and y specify the new position of the top-left corner, and width and height specify the new size.
*/
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
/*
* Create a text field for user input
*/
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
// Text field for entering 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 entered information is replaced with dots for the security of the password
*/
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
// Create a login button
JButton loginButton = new JButton("login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
}
}
Execute the following commands for the output:
$ javac SwingLoginExample.java
$ java SwingLoginExample
Concept Analysis:
JFrame – The basic