How to create Swing program in java

import javax.swing.*;

public class FirstSwingExample 
{
    public static void main(String[] args) 
    {
        JFrame f = new JFrame(); // creating instance of JFrame
        JButton b = new JButton("click"); // creating instance of JButton
        
        b.setBounds(130, 100, 100, 40); // x axis, y axis, width, height
        f.add(b); // adding button in JFrame
        
        f.setSize(400, 500); // 400 width and 500 height
        f.setLayout(null); // using no layout managers
        f.setVisible(true); // making the frame visible
    }
}

Step-by-Step Explanation

1. Import Statement:

import javax.swing.*;

This import statement brings into the program javax.swing.* package. This package brings in a myriad of Swing components such as JFrame, JButton, JTextArea, JButton, JTextField etc

Swing components are basically used to generate GUIs of a Java Program.

2. Class Declaration:

public class FirstSwingExample {

The class FirstSwingExample is defined. It has the main method and the code for creating and showing the GUI.

3. Main Method:

public static void main(String[] args) {

The main() method is the entry point of the program. This is where the execution begins.

4. Creating a JFrame (Window):

JFrame f = new JFrame(); // creating instance of JFrame

  • A new JFrame object f is created. JFrame represents the main window (or frame) where GUI components can be added.
  • A JFrame is like a container that holds various components like buttons, labels, and text fields.

5. Creating a JButton (Button):

JButton b = new JButton(“click”); // creating instance of JButton

  • A new JButton object b is created with the label “click”. This button is what the user can click in the window.
  • The button is initially displayed with the text “click” on it.

6. Setting the Position and Size of the Button:

b.setBounds(130, 100, 100, 40); // x axis, y axis, width, height

  • The setBounds() method sets the position and size of the button.
  • The arguments (130, 100, 100, 40) point to the coordinates and size for the button in the panel.
  • The horizontal position on the x-axis is 130.
  • The y-axis position is 100 (vertical position).
  • The width of the button is 100 pixels.
  • The height of the button is 40 pixels.

7. Adding the Button to the JFrame:

f.add(b); // adding button in JFrame

The add() method adds the button b to the frame f. It makes the button show inside the window.

8. Setting the Size of the JFrame:

f.setSize(400, 500); // 400 width and 500 height

The setSize() method defines the window’s size. Here, it’s set as follows: 400 pixels of width and 500 pixels of height.

9. With No Layout Managers

f.setLayout(null); // without any layout manager

  • The setLayout(null) method disables the use of a layout manager. As such, one has to specify an absolute position of the component like the button.
  • Without a layout manager, you have to set the position and size of each component manually by using methods such as setBounds().

10. Making the JFrame Visible:

f.setVisible(true); // making the frame visible

The setVisible(true) method is invoked to make the frame visible on the screen. If this method is not invoked, the frame will not appear on the screen.

Summary of Operations:

  • A window is created, JFrame.
  • A button is created, JButton, with the label “click”.
  • The button is placed at coordinates (130, 100) with a size of 100×40 pixels.
  • The button is added to the window (frame).
  • Window size is set to 400×500 pixels.
  • Layout manager is set to null. This means any components inside it will be absolutely positioned.
  • Finally, the window is visible.

Output:

When this program runs, the following happens:

1. A window (frame) of size 400×500 pixels will be displayed

2. Inside the window, at position (130, 100) there will be a button labeled “click” of size 100×40 pixels.

3. The window will remain open until it is closed by the user.

Here is a simple representation of the output:

———————————————————–

|                                                         |

|                                                         |

|                                                         |

|                     [click]                            |

|                                                         |

|                                                     |

———————————————————–

Window Dimension: 400×500 pixels

Key Points:

  • JFrame is used as the main window.
  • JButton is the clickable button that appears in the window.
  • The setBounds(x, y, width, height) method is used for absolute positioning of the button.
  • The setLayout(null) method is used to disable the layout manager allowing manual positioning.
  • The setSize(width, height) method is used to set up the size of the window.

This is a simple GUI program, and you can enhance this by adding an event listener such that when you click the button, it does something. You could show a message when you press the button using an ActionListener.

Leave a Comment

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