Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Swing: Creating graphical user interfaces (GUI) in Java

The GUI components are part of the Java toolkit known as Swing for Graphical User Interfaces (GUI). Java programs can use a wide range of widgets and packages to build powerful GUI components using Swing. Swing is a component of the Java Foundation Classes (JFC), an API for Java GUI programming that offers GUI. 

The Java Swing library serves as the foundation for the more dated, platform-specific Java Abstract Widget Toolkit (AWT). You can use the straightforward GUI programming components from the library, such as buttons, textboxes, etc., rather than constructing the components from scratch. Get started with the Best Java Training in Chennai at SLA Institute to gain expertise in Swing concepts. 

The diagram of a Swing Class

What does "Container Class" mean?

Classes called container classes are classes that can hold other parts. Thus, we require at least one container object in order to create a Java Swing GUI. Java Swing containers are available in three different types.

Panel: It is only a container and not a window in and of itself. The only purpose of a Panel is to arrange the window’s components.

Frame: The window is fully functioning and contains a title and icons.

Dialog: When a message needs to be displayed, it functions similarly to a pop-up window. It is not a completely working window, just like the frame.

What is a GUI in Java?

The Graphical User Interface (GUI) of Java is a straightforward tool for developing visual experiences for Java programs. It is mostly formed of graphical elements that allow the user to interact with a program, such as buttons, labels, windows, etc. A GUI is crucial in creating user-friendly interfaces for Java programs.

An Example of a Java GUI’s Creation

Let’s learn how to develop a GUI in Java using examples from Swings in this Java GUI tutorial. Go through our blog that contains many simple and sample Java programs.

Step 1: Paste the code into an editor.

Copy the following code into an editor as a first step.

import javax.swing.*;

class gui{

    public static void main(String args[]){

       JFrame frame1 = new JFrame(“My First GUI”);

       frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame1.setSize(300,300);

       JButton button = new JButton(“Press”);

       frame1.getContentPane().add(button); // Adds Button to content pane of frame

       frame1.setVisible(true);

    }

}

Step 2: Execute the code

The code must then be saved, compiled, and executed.

Step 3: Paste the code below into an editor.

Add a button to our frame now. Copy the following code from the provided Java UI Example into an editor.

import javax.swing.*;

   class gui{

      public static void main(String args[]){

        JFrame frame1 = new JFrame(“My First GUI”);

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setSize(300,300);

       JButton button1 = new JButton(“Press”);

       frame1.getContentPane().add(button1);

       frame1.setVisible(true);

     }

}

Step 4: Run the program.

“Paraphrase” is a code. You’ll receive a sizable button.

Step 5: Provide two buttons.

Consider including two buttons. Into an editor, copy the code below.

import javax.swing.*;

class gui{

      public static void main(String args[]){

           JFrame frame1 = new JFrame(“My First GUI”);

           frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           frame1.setSize(300,300);

          JButton button1 = new JButton(“Button 1”);

          JButton button2 = new JButton(“Button 2”);

          frame1.getContentPane().add(button1);

          frame1.getContentPane().add(button2);

          frame1.setVisible(true);

     }

}

Step 6: Save the application and run it.

Save, compile, and run the program after that.

Step 7: Verify the results

Unanticipated results =? Overlapping buttons are happening.

Layout Manager in Java

The layout manager is used to arrange the GUI Java components inside a container. Know the importance of learning Java through our recent blog. There are more layout managers, but these are the most widely used ones:

BorderLayout in Java

A BorderLayout allows for the placement of components in the top, bottom, left, right, and center of the layout. Java JFrame is every java JFrame manager.

FlowLayout in Java

FlowLayout is the default layout manager used by all JPanels. It merely organizes the components in a single row, sequentially.

GridBagLayout in Java

That is the most sophisticated layout of all. When components are arranged within a grid of cells, they can cover a wide range of cells and yet be correctly aligned.

Step 8: Build a conversation frame

Why not try making a chat frame like the one below?

Sample Code to Try

import javax.swing.*;

import java.awt.*;

class gui {

    public static void main(String args[]) {

        //Creating the Frame

        JFrame frame1 = new JFrame(“Chat Frame”);

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setSize(400, 400);

        //Creating the MenuBar and adding components

        JMenuBar mb = new JMenuBar();

        JMenu m1 = new JMenu(“FILE”);

        JMenu m2 = new JMenu(“Help”);

        mb.add(m1);

        mb.add(m2);

        JMenuItem m11 = new JMenuItem(“Open”);

        JMenuItem m22 = new JMenuItem(“Save as”);

        m1.add(m11);

        m1.add(m22);

        //Creating the panel at bottom and adding components

        JPanel panel = new JPanel(); // the panel is not visible in output

        JLabel label = new JLabel(“Enter Text”);

        JTextField tf = new JTextField(10); 

        JButton send = new JButton(“Send”);

        JButton reset = new JButton(“Reset”);

        panel.add(label); 

        panel.add(tf);

        panel.add(send);

        panel.add(reset);

        // Text Area at the Center

        JTextArea ta = new JTextArea();

        frame1.getContentPane().add(BorderLayout.SOUTH, panel);

        frame1.getContentPane().add(BorderLayout.NORTH, mb);

        frame1.getContentPane().add(BorderLayout.CENTER, ta);

        frame1.setVisible(true);

    }

}

Bottom Line

Java’s Swing is a simple GUI toolkit with a large selection of widgets for creating efficient window-based applications. It belongs to the JFC ( Java Foundation Classes). It is entirely constructed using the AWT API and written in Java. It has lightweight components and is platform neutral, in contrast to AWT. Gain expertise with GUI concepts using Java Swing by enrolling in our Java Course in Chennai at SLA Institute.

The post Java Swing: Creating graphical user interfaces (GUI) in Java appeared first on Software Training Institute in Chennai with 100% Placements - SLA Institute.



This post first appeared on AWS Training In Chennai SLA Institutes, please read the originial post: here

Share the post

Java Swing: Creating graphical user interfaces (GUI) in Java

×

Subscribe to Aws Training In Chennai Sla Institutes

Get updates delivered right to your inbox!

Thank you for your subscription

×