This is the 23rd day of my participation in the First Challenge 2022

Java Programming graphical User Interface

The container

In addition to the JFrame representation, there are several other common forms: JPanel, JSplitPane, JTabbedPane, JScrollPane, JDesktopPane, JInternalFrame, and so on

JPanel

JPanel is also one of the most commonly used containers, which can be used to complete a variety of complex interface display. You can add any component to a JPanel, and then add the JPanel container directly to the JFrame container to display

methods role
public JPanel() Create a default JPanel object, using flow layout management
public JPanel(LayoutManager layout) Creates a JPanel object specifying the layout manager
import javax.swing.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        JPanel pan = new JPanel();
        pan.add(new JLabel("A"));
        pan.add(new JButton("A"));
        pan.add(new JLabel("B"));
        pan.add(new JButton("B"));
        frame.add(pan);
        frame.pack();
        frame.setLocation(300.200);
        frame.setVisible(true); }}Copy the code

All components are added sequentially to the JPanel

JSplitPane

The main function of JSplitPane is to split the panel, which can divide a form into two sub-forms, either horizontally or vertically

constant

constant role
public static final int HORIZONTAL_SPLIT Represents horizontal segmentation
public static final int VERTICAL_SPLIT Represents vertical segmentation

methods

methods role
public JSplitPane(int newOrientation) Create an object and specify how to split it
public JSplitPane(int newOrientation,boolean new ContinuousLayout,Component newLeftComponent,Component newRightlLocation) Creates objects that specify how to split, whether the splitter bar changes to redraw the image, and display components at both ends
public void setDividerLocation(double proportionalLocation) Sets the position of the splitter bar, by percentage
public void setOneTouchExpandable(boolean newValue) Set whether to provide quick expansion and collapse functions
public void setDividerSize(int newSize) Set the splitter bar size
import javax.swing.*;
import java.awt.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        Container cont = frame.getContentPane();
// Get the form container
        JSplitPane lf = null;
        JSplitPane tb = null;
        lf=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JLabel("Left"),new JLabel("Right"));
        tb=new JSplitPane(JSplitPane.VERTICAL_SPLIT,lf,new JLabel("Under"));
        tb.setDividerSize(10);
        tb.setOneTouchExpandable(true);
        cont.add(tb);
        frame.setSize(300.200);
        frame.setLocation(300.200);
        frame.setVisible(true); }}Copy the code

The length of the partition line on the left and right panels is relatively small

The length of the dividing line of the upper and lower panel is larger

Has quick expansion/collapse function

JTabbedPane

JTabbedPane is a panel with multiple tabs

constant

constant role
static final int TOP Indicates the position pointing to the top of the box
static final int BOTTOM Indicates the position at the bottom of the box
static final int LEFT It points to the left of the box
static final int RIGHT Indicates the position to the right of the frame

methods

methods role
public JTabbedPane(int tabPlancement) Creates the object and specifies the TAB layout
public void addTab(String title,Component compioent) Add a component with a title and no icon
public void addTab(String title,Icon icon,Component component) Add a component with a title and an icon
public void addTab(String title,Icon icon,Component component,String tip) Add a component with a title, icon, and prompt
import javax.swing.*;
import java.awt.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        Container cont = frame.getContentPane();
        JTabbedPane tab = null;
        tab = new JTabbedPane(JTabbedPane.TOP);
        JPanel p1= new JPanel();
        JButton b1 = new JButton("1");
        p1.add(b1);
        JPanel p2= new JPanel();
        JButton b2 = new JButton("2");
        p2.add(b2);
        tab.addTab("一",p1);
        tab.addTab("二",p2);
        cont.add(tab);
        frame.setSize(300.200);
        frame.setLocation(300.200);
        frame.setVisible(true); }}Copy the code

Two tabs are created and placed at the top of the form

Each TAB adds a JPanel panel

JScrollPane

The main function is to add horizontal scroll bars to display content

constant role
static final int HORIZONTAL_SCROLLBAR_ALWAYS Always display a horizontal scroll bar
static final int HORIZONTAL_SCROLLBAR_NEVER Horizontal scrollbars are not displayed under any circumstances
static final int HORIZONTAL_SCROLLBAR_AS_NEEDED Display horizontal scroll bars according to your needs
static final int VERTICAL_SCROLLBAR_ALWAYS Always display a vertical scroll bar
static final int VERTICAL_SCROLLBAR_NEVER Do not display a vertical scroll bar under any circumstances
static final int VERTICAL_SCROLLBAR_AS_NEEDED Displays a vertical scroll bar based on your needs
methods role
public JScrollPane(Component view) Adds the specified component to the scrollbar to display the scrollbar by size
public JScrollPane(Component view,int vsbPolicy,int hsbPolicy) Adds the specified component to the scrollbar, setting whether to display the scrollbar as required
public void setHorIzontalScrollBarPolicy(int policy) Sets the display policy for the horizontal scroll bar
public void setVerticalScrollBarPolicy(int policy) Sets the display policy for the vertical scroll bar
import javax.swing.*;
import java.awt.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        Container cont = frame.getContentPane();
        String Path="C:\\Users\\30452\\Desktop\\123.jpg";
        Icon icon = new ImageIcon(Path);
        JPanel pan = new JPanel();
        JLabel lab = new JLabel(icon);
        pan.add(lab);
        JScrollPane s = null;
        s = new JScrollPane(pan,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        cont.add(s);
        frame.setSize(200.500);
        frame.setLocation(300.200);
        frame.setVisible(true); }}Copy the code

JDesktopPane and JInternalFrame

JDesktopPane specifies the basic form of a parent form, and JInternalFrame specifies the child forms to which jInternalFrames need to be added

Common methods of the JDesktopPane class

methods role
public JDesktopPane() Create a JDesktopPane() object
public void setSelectedFrame(JInternalFrame f) Sets the JInternalFrame currently active in this JDesktopPane

Common methods of the JInternalFrame class

methods role
public JInternalFrame(String title) Creates a non-resizable, non-closed, non-maximized, non-iconified JInternalFrame with the specified title
public JInternalFrame(String title,boolean resizable) Creates a non-closable, non-maximized, non-iconicized, resized, and captioned JInternalFrame
public JInternalFrame(String title,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable) Creates a resizable, closeable, maximizable, non-iconizable, JInternalFrame with the specified title
import javax.swing.*;
import java.awt.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        frame.setLayout(new BorderLayout());
        Container cont = frame.getContentPane();
        JDesktopPane desk = new JDesktopPane();
        cont.add(desk,BorderLayout.CENTER);
        JInternalFrame jif = null;
        String Path = "C:\\Users\\30452\\Desktop\\123.jpg";
        Icon icon = new ImageIcon(Path);
        JPanel pan = null;
        for(int i=0; i<2; i++){ jif =new JInternalFrame("test".true.true.true.true);
            pan = new JPanel();
            pan.add(new JLabel(icon));
            jif.setLocation(35-i*20.35-i*20);
            jif.add(pan);
            jif.pack();
            jif.setVisible(true);
            desk.add(jif);
        }
        frame.setSize(600.800);
        frame.setLocation(300.200);
        frame.setVisible(true); }}Copy the code