java Swing graphical interface

People who have studied Java should be very disgusted with the graphical interface of Java, especially those who have been exposed to Java for a short time. If you want to drag with the mouse like other languages, you can use the wondersbulider plug-in. But it’s not that convenient to use. Of course, it is really a blessing for people who are not willing to write code.

However, it is not that troublesome to use code to implement Java’s graphical interface. To sum it up~

1. Define the panels, buttons, text, and labels you need to use.

2. Instantiate these. Then place the buttons, text, and labels on the panel the way you want.

3. Place the panel on the interface. If the interface is simple, the panel does not need to be used. The panel can facilitate the management of some buttons.

How to simply add some buttons?

Define panel buttons~instantiate panel buttons~add buttons to the panel.

package Swing;
import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameDemo extends JFrame {
    private JPanel jp,jp1; //Declaration panel, panel function: button text can be added, etc. to facilitate management interface.
    private JButton b1; //Declaration button
    private JButton b2,b3;
    publicFrameDemo()
    {
        super("test window");
        jp = new JPanel();
        b1 = new JButton("Button 1");
        b2 = new JButton("Button 2");//Instantiation jp b1 b2
        jp.add(b1);
        jp.add(b2);
    
        
        this.add(jp); //Buttons are added to the panel, and panels are added to the interface.
        this.setSize(300,200); //Set the window width to 300 and the height to 200
        this.setLocation(100,100); //Set the coordinates of the upper left corner of the window and the position when the window is opened. Set the upper left fixed point as 0.0
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
         FrameDemo frame = new FrameDemo();
         frame.setVisible(true);
              
    }

}

Here are some layouts to make your interface more beautiful:

Flow layout ~ components are arranged on the panel from left to right. Also the default layout

package Swing;
import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameDemo extends JFrame {
    private JPanel jp; //Declaration panel, panel function: button text can be added, etc. to facilitate management interface.
    private JButton b1; //Declaration button
    private JButton b2,b3,b4;
    publicFrameDemo()
    {
        super("test window");
        jp = new JPanel(); // Instantiate the panel first, and then layout the panel. (Align left, horizontal spacing 10, vertical spacing 15) Can be changed.
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT,10,25);
        jp.setLayout(layout);
         
        b1 = new JButton("Button 1");
        b2 = new JButton("Button 2");
        b3 = new JButton("Button 3");
        b4 = new JButton("Button 4");
        jp.add(b1);
        jp.add(b2);
        jp.add(b3);
        jp.add(b4);
        this.add(jp); //The button is added to the panel and the panel is added to the interface
        this.setSize(300,200); //Set the window width to 300 and the height to 200
        this.setLocation(100,100); //Set the coordinates of the upper left corner of the window and the position when the window is opened.
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
         FrameDemo frame = new FrameDemo();
         frame.setVisible(true);
              
    }

}

Results of the

Border layout ~ There are five positions, southeast, northwest, center ~ the default is center

package Swing;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameDemo extends JFrame {
    private JPanel jp; //Declaration panel, panel function: button text can be added, etc. to facilitate management interface.
    private JButton b1; //Declaration button
    private JButton b2,b3,b4;
    publicFrameDemo()
    {
        super("test window");
        jp = new JPanel(); // Instantiate the panel first, and then layout the panel. (Align left, horizontal spacing 10, vertical spacing 15) Can be changed.
        jp.setLayout(new BorderLayout());
         
        b1 = new JButton("Button 1");
        b2 = new JButton("Button 2");//Instantiation jp b1 b2
        b3 = new JButton("Button 3");
        b4 = new JButton("Button 4");
        jp.add(b1,BorderLayout.WEST);
        jp.add(b2,BorderLayout.WEST); //Button 2 covers button 1. If the button size is not set, the length of the button will be the length of the panel.
        jp.add(b3,BorderLayout.NORTH);
        jp.add(b4,BorderLayout.SOUTH);
        this.add(jp); //The button is added to the panel and the panel is added to the interface
        this.setSize(500,500); //Set the window width to 500 and the height to 500
        this.setLocation(100,100); //Set the coordinates of the upper left corner of the window and the position when the window is opened.
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
         FrameDemo frame = new FrameDemo();
         frame.setVisible(true);
              
    }

}<br><br>

Note that button 1 is covered by button 2

Grid layout and card layout are the same setup steps, the only thing that changes is the layout method. As long as you add components in sequence and pay attention to the position of each component, you can create the interface you require.

My favorite layout~null layout. You can add them as you wish on the panel, but it can easily cause layout confusion. I put the text box, label, and button in the null layout. The label and text box are placed in the same way as the button.

package Swing;
import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class FrameDemo extends JFrame {
    private JPanel jp; //Declaration panel, panel function: button text can be added, etc. to facilitate management interface.
    private JButton b1; //Declaration button
    private JButton b2,b3,b4;
    private JLabel lname ,lpwd;
    private JTextField txtname;
    private JPasswordField txtpwd;
     publicFrameDemo()
    {
        super("test window");
        jp = new JPanel();
        jp.setLayout(null);
        b1 = new JButton("Login"); //Add button creation
        b2 = new JButton("Cancel");

        b1.setBounds(60, 90, 60, 25);
        b2.setBounds(125, 90, 60, 25);
        jp.add(b1);
        jp.add(b2);

        lname = new JLabel("Username"); //Add to label creation
        lpwd = new JLabel("password");
        txtname = new JTextField(20);
        txtpwd = new JPasswordField(20);
        txtpwd.setEchoChar('*');
        
        lname.setBounds(30,30,60,25);
        txtname.setBounds(95, 30, 120, 25);
        lpwd.setBounds(30,60,60,25);
        txtpwd.setBounds(95,60,120,25);
        
        jp.add(lname);
        jp.add(txtname);
        jp.add(lpwd);
        jp.add(txtpwd);
        
        this.add(jp); //The button is added to the panel and the panel is added to the border'
        this.setSize(250,170);
        this.setLocation(300,300); //Set the coordinates of the upper left corner of the window and the position when the window is opened.
        this.setResizable(false);//The window cannot be resized
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
         FrameDemo frame = new FrameDemo();
         frame.setVisible(true);
              
    }

}

Will you install some components yourself? The steps for adding check boxes are the same, but you should pay attention to setting up more panels to manage the labels of each check box, etc.