Summary of Java layout manager
FlowLayout
explain
This layout manager is a flow layout manager, which will make all components arranged like a flow, from left to right and from top to bottom. If the arrangement fails, it will wrap lines. If the window is opened, it will continue to be arranged like a flow.
Common methods and constants
Serial number | Methods and constants | type | describe |
---|---|---|---|
1 | public static final int CENTER | constant | Center alignment |
2 | public static final int LEADING | constant | Same as the beginning of the container |
3 | public static final int LEFT | constant | Align left |
4 | public static final int RIGHT | constant | Right align |
5 | public static final int TRAILING | constant | Same as the end of the container |
6 | public FlowLayout() | structure | Construct a new FlowLayout, align it in the center, and the default horizontal and vertical spacing is 5 units |
7 | public FlowLayout(int align)__ | structure | Construct a FlowLayout and specify the alignment |
8 | public FlowLayout(int align.int bgap,int vgap) | structure | Specify alignment, horizontal, vertical spacing |
Code example
package com.learnspringboot.libra; import javax.swing.*; import java.awt.*; /** * FlowLayoutTest01 class * Date: November 22, 2021 * Time: 12:42 * * @author Don't bully Xiaobai, okay */ public class FlowLayoutTest01 { public static void main(String[] args) { JFrame frame = new JFrame("Welcome!"); // The last two parameters, hgap and vgap, set the gap between components. frame.setLayout(new FlowLayout(FlowLayout.CENTER,3,3)); JButton but = null; int end = 9; for (int i = 0; i < end; i++) { but = new JButton("Button --> " + i); frame.add(but); } Toolkit tk = Toolkit.getDefaultToolkit(); Dimension size = tk.getScreenSize(); // Sets the size of the window int FrameWidth = 350; int FrameHeight = 130; // Set the position of the window. This calculation will make it in the center of the screen int screenHeight = size.height / 2 - FrameHeight / 2; int screenWidth = size.width / 2 - FrameWidth / 2; // Sets the position of the window frame.setLocation(screenWidth,screenHeight); frame.setSize(FrameWidth,FrameHeight); // Add an event listener, which means that the program exits when the window is closed, so you don't have to exit the program every time the window is closed. frame.addWindowListener(new MyWindowListener()); frame.setVisible(true); } }
Operation results
BorderLayout
Common methods and constants
Serial number | Methods and constants | type | Description |
---|---|---|---|
1 | public static final String EAST | constant | Set component in East region |
2 | public static final String WEST | constant | Set component in West Region |
3 | public static final String SOUTH | constant | Set the component in the south area |
4 | public static final String NORTH | constant | Set component in North Region |
5 | public static final String CENTER | constant | Set components in area |
6 | public Border ayout() | structure | Construct a layout without distance |
7 | public BorderLayout(int hgap,int vgap) | structure | Construct a layout with horizontal and vertical spacing |
explain
The layout manager will divide a form into five parts, namely, East, West, North and south. All components will be placed in these five areas. You can directly set which area to put in. If you don't specify it, it will be placed in the middle area by default.
Code example
package com.learnspringboot.libra; import javax.swing.*; import java.awt.*; /** * BorderLayoutTest01 class * Date: November 22, 2021 * Time: 13:21 * * @author Don't bully Xiaobai, okay */ public class BorderLayoutTest01 { public BorderLayoutTest01(String title){ JFrame frame = new JFrame(title); frame.setLayout(new BorderLayout(3,3)); frame.add(new JButton("East( EAST)"),BorderLayout.EAST); frame.add(new JButton("West( WEST)"),BorderLayout.WEST); frame.add(new JButton("South( SOUTH)"),BorderLayout.SOUTH); frame.add(new JButton("North( NORTH)"),BorderLayout.NORTH); frame.add(new JButton("Medium( CENTER)"),BorderLayout.CENTER); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension size = tk.getScreenSize(); // Sets the size of the window int FrameWidth = 350; int FrameHeight = 130; // Set the position of the window. This calculation will make it in the center of the screen int screenHeight = size.height / 2 - FrameHeight / 2; int screenWidth = size.width / 2 - FrameWidth / 2; // Sets the position of the window frame.setLocation(screenWidth,screenHeight); frame.setSize(FrameWidth,FrameHeight); // Sets the position of the window frame.setLocation(screenWidth,screenHeight); frame.setSize(FrameWidth,FrameHeight); frame.addWindowListener(new MyWindowListener()); frame.setVisible(true); } public BorderLayoutTest01(){ this("Hello World!"); } }
Operation results
GridLayout
Common methods and constants
Serial number | method | type | Description |
---|---|---|---|
1 | public GridLayout(int rows,int cols) | structure | Construct a layout manager that specifies rows and columns |
2 | public GridLayout(int rows,int cols,int hgap,int vgap) | structure | Specify row and column, horizontal and vertical spacing when constructing |
explain
The layout manager is a table manager. It is a table that divides the window into specific rows and columns, and then each component occupies a small grid in it.
Code example
package com.learnspringboot.libra; import javax.swing.*; import java.awt.*; /** * GridLayoutTest01 class * Date: November 22, 2021 * Time: 13:50 * * @author Don't bully Xiaobai, okay */ public class GridLayoutTest01 { public GridLayoutTest01(String title){ JFrame frame = new JFrame(title); frame.setLayout(new GridLayout(3,5,3,3)); JButton but = null; int end = 15; for (int i = 0; i < end; i++) { but = new JButton("Button --> " + i); frame.add(but); } Toolkit tk = Toolkit.getDefaultToolkit(); Dimension size = tk.getScreenSize(); int FrameWidth = 600; int FrameHeight = 200; int screenHeight = size.height / 2 - FrameHeight / 2; int screenWidth = size.width / 2 - FrameWidth / 2; frame.setLocation(screenWidth,screenHeight); frame.setSize(FrameWidth,FrameHeight); frame.addWindowListener(new MyWindowListener()); frame.setVisible(true); } public GridLayoutTest01(){ this("Welcome!"); } }
Operation results
Calculator instance
Made a calculator window
The following is the source code
package com.learnspringboot.libra.calculator; import javax.swing.*; import java.awt.*; /** * SetFrame class * Date: November 24, 2021 * Time: 19:27 * * @author Don't bully Xiaobai, okay */ public class SetFrame { public SetFrame(JFrame frame,int FrameWidth,int FrameHeight){ Toolkit tk = Toolkit.getDefaultToolkit(); Dimension size = tk.getScreenSize(); int screenHeight = size.height / 2 - FrameHeight / 2; int screenWidth = size.width / 2 - FrameWidth / 2; frame.setLocation(screenWidth,screenHeight); frame.setSize(FrameWidth,FrameHeight); } }
package com.learnspringboot.libra.calculator; /** * Application class * Date: November 22, 2021 * Time: 13:21 * * @author Don't bully Xiaobai, okay */ public class Application { public static void main(String[] args) { new MyFrameDraw(); } }
package com.learnspringboot.libra.calculator; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; /** * MyWindowListener class * Date: November 22, 2021 * Time: 12:51 * * @author Don't bully Xiaobai, okay */ public class MyWindowListener implements WindowListener { @Override public void windowOpened(WindowEvent e) { } @Override public void windowClosing(WindowEvent arg0){ System.out.println("window closing"); System.exit(1); } @Override public void windowClosed(WindowEvent e) { } @Override public void windowIconified(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowActivated(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } }
package com.learnspringboot.libra.calculator; import javax.swing.*; /** * MyPanel class * Date: November 24, 2021 * Time: 18:47 * * @author Don't bully Xiaobai, okay */ public class MyPanelDraw { public MyPanelDraw(){ } public JPanel getPanel(){ JPanel jPanel = new JPanel(); return jPanel; } }
package com.learnspringboot.libra.calculator; import com.learnspringboot.libra.frame.MyWindowListener; import javax.swing.*; import java.awt.*; /** * MyFrameDraw class * Date: November 24, 2021 * Time: 7:59 * * @author Don't bully Xiaobai, okay */ public class MyFrameDraw { public MyFrameDraw(int FrameWidth, int FrameHeight){ JFrame frame = new JFrame(); new SetFrame(frame,FrameWidth,FrameHeight); frame.setLayout(null); frame.add(getPanel01()); frame.add(getPanel02()); frame.add(getPanel03()); frame.setVisible(true); frame.addWindowListener(new MyWindowListener()); } public JPanel getPanel01(){ JPanel jPanel = new JPanel(new FlowLayout()); JTextField text = new JTextField(17); JButton cleanButton = new JButton("="); jPanel.add(text); jPanel.add(cleanButton); jPanel.setBounds(10,0,290,50); return jPanel; } public JPanel getPanel02(){ JPanel jPanel = new JPanel(new GridLayout(3,3)); int end = 10; for (int i = 1; i < end; i++) { JButton but = new JButton(""+i); jPanel.add(but); } jPanel.setBounds(10,50,290,100); return jPanel; } public JPanel getPanel03(){ JPanel jPanel = new JPanel(new GridLayout(2,3)); jPanel.add(new Button(" 0")); jPanel.add(new Button(" +")); jPanel.add(new Button(" -")); jPanel.add(new Button(" *")); jPanel.add(new Button(" /")); jPanel.add(new Button(" .")); jPanel.setBounds(10,150,290,100); return jPanel; } public MyFrameDraw(){ this(325,300); } }
Summary
This article ends here. If you like, please praise and support. Thank you~