Java Swing Tutorial 2

Java Swing Tutorial 2

In our next model, we will have a catch. When we tap on the catch, the application ends.

package com.softwarelogy;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

public class QuitButtonEx extends JFrame {

    public QuitButtonEx() {

        initUI();
    }

    private void initUI() {

        var quitButton = new JButton("Quit");

        quitButton.addActionListener((event) -> System.exit(0));

        createLayout(quitButton);

        setTitle("Quit button");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        var pane = getContentPane();
        var gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            var ex = new QuitButtonEx();
            ex.setVisible(true);
        });
    }
}

We position a JButton on the window and add an activity audience to this catch.

var quitButton = new JButton(“Quit”);

Here we make a catch part. This constructor takes a string name as a parameter.


quitButton.addActionListener((event) -> System.exit(0));

We plug an activity audience to the catch. The activity ends the application by calling the System.exit() strategy.


createLayout(quitButton);

The youngster parts should be put into holders. We delegate the undertaking to the createLayout() strategy.


var pane = getContentPane();

var gl = new GroupLayout(pane);pane.setLayout(gl);

The substance sheet of a JFrame is a region where tyke parts are put. The youngsters are sorted out by specific non-unmistakable parts called design administrators. The default design administrator of a substance sheet is the BorderLayout supervisor. This supervisor is exceptionally basic and is helpful just in specific cases. In this instructional exercise, we utilize the GroupLayout supervisor which is all the more ground-breaking and adaptable.


gl.setAutoCreateContainerGaps(true);

The setAutoCreateContainerGaps() technique makes holes among segments and the edges of the holder. Space or holes are essential piece of the structure of every application.

gl.setHorizontalGroup(gl.createSequentialGroup() .addComponent(arg[0]));

gl.setVerticalGroup(gl.createSequentialGroup() .addComponent(arg[0]));

GroupLayout supervisor characterizes the format for each measurement autonomously. In one stage, we spread out segments close by the even hub; in the other advance, we spread out segments along the vertical hub. In the two sorts of formats we can mastermind segments successively or in parallel. In a level format, a line of parts is known as a consecutive gathering and a segment of segments is known as a parallel gathering. In a vertical format, a segment of segments is known as a consecutive gathering and a column of segments a parallel gathering.

In our model we have just a single catch, so the design is extremely basic. For each measurement, we call the addComponent() strategy with the catch segment as a parameter. (Every youngster segment must be included for the two measurements.)

softwarelogy

Leave a Reply

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