Java

Java – Swing Tutorial For Beginners

Java Foundation Classes (JFC) includes Swing together with Java 2D and Abstract Window Toolkit (AWT). Java uses AWT, Swing, and Java 2D to create graphical user interfaces (GUIs). This course will focus mostly on the Swing API, which is substantially more lightweight than AWT and is used to develop GUIs on top of AWT.

A Simple swing example

Several swing components that you have not yet learned in this lesson will be used in the example below. In the next swing lessons, we will go over everything in depth.
A login screen would be generated using the swing program below.

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 SwingFirstExample {
    
    public static void main(String[] args) {    
        // Creating instance of JFrame
        JFrame frame = new JFrame("My First Swing Example");
        // Setting the width and height of frame
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* Creating panel. This is same as a div tag in HTML
         * We can create several panels and add them to specific 
         * positions in a JFrame. Inside panels we can add text 
         * fields, buttons and other components.
         */
        JPanel panel = new JPanel();    
        // adding panel to frame
        frame.add(panel);
        /* calling user defined method for adding components
         * to the panel.
         */
        placeComponents(panel);

        // Setting the frame visibility to true
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        /* We will discuss about layouts in the later sections
         * of this tutorial. For now we are setting the layout 
         * to null
         */
        panel.setLayout(null);

        // Creating JLabel
        JLabel userLabel = new JLabel("User");
        /* This method specifies the location and size
         * of component. setBounds(x, y, width, height)
         * here (x,y) are cordinates from the top left 
         * corner and remaining two arguments are the width
         * and height of the component.
         */
        userLabel.setBounds(10,20,80,25);
        panel.add(userLabel);

        /* Creating text field where user is supposed to
         * enter user name.
         */
        JTextField userText = new JTextField(20);
        userText.setBounds(100,20,165,25);
        panel.add(userText);

        // Same process for password label and text field.
        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(10,50,80,25);
        panel.add(passwordLabel);

        /*This is similar to text field but it hides the user
         * entered data and displays dots instead to protect
         * the password like we normally see on login screens.
         */
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100,50,165,25);
        panel.add(passwordText);

        // Creating login button
        JButton loginButton = new JButton("login");
        loginButton.setBounds(10, 80, 80, 25);
        panel.add(loginButton);
    }

}

Output:

Swing Login Screen Example

In the example above, we utilized a number of them. Let’s talk a little about them now, and in the subsequent tutorials, we’ll go into more detail.
A frame is an instance of the JFrame class. A window’s frame may have a title, border, menu, buttons, text fields, and other elements. A frame is required for a Swing application before any components can be added.

A panel is an instance of the JPanel class. A frame may include many panels, each of which may have a number of parts. They may also be referred to as Frame components. Panels are helpful for organizing elements and positioning them in the right places in a frame.

JLabel – An instance of the JLabel class is a label. Unselectable text and photos make up a label. Using labels will allow you to show a string or a picture on a frame. In the example above, we wanted to display the words “User” and “Password” immediately before the text fields. To achieve this, we created labels and added them to the proper positions.

JTextField – Used to record user inputs, these text fields are where users submit information.

Similar to text fields, JPasswordField hides the entered data and displays it as dots on the GUI instead.

JButton – An instance of the JButton class is a button. The word “Login” is on a button in the example above.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button