Home >Java >javaTutorial >Three common layout methods and their uses in Java GUI
All components are discharged one by one like a flow. After one row is filled, the next row is rowed. By default, each component is It is arranged in the center, but it can also be set.
Construction method of flow layout:
new FlowLayout();
new FlowLayout(int alignment);//Set the alignment (default is FlowLayout.CENTER Centered), we usually change to FlowLayout.LEFT
new FlowLayout(int aligment,int horizGap,int vertGap);//Set the alignment upper and lower offset
aligment value:
FlowLayout.LEFT = 0
FlowLayout.CENTER = 1
FlowLayout.RIGHT = 2
Set through the setLayout function Layout
For example:jf.setLayout(new FlowLayout(FlowLayout.LEFT));
Border layout is the default layout management method. Border layout divides the container into east (BorderLayout.EAST), west (BorderLayout.WEST), south (BorderLayout.SOUTH), north (BorderLayout.NORTH), middle (BorderLayout) .CENTER) 5 areas
Specify the content when new
Specify the boundary when adding components to the JFrame container
Example:
import javax.swing.*; import java.awt.*; public class Borderlayout{ public static void main(String[] args) { JFrame jf = new JFrame(); jf.setLayout(new BorderLayout()); JButton east = new JButton("east"); JButton west = new JButton("west"); JButton south = new JButton("south"); JButton north = new JButton("north"); JButton center = new JButton("center"); jf.add(east,BorderLayout.EAST); jf.add(west,BorderLayout.WEST); jf.add(south,BorderLayout.SOUTH); jf.add(north,BorderLayout.NORTH); jf.add(center,BorderLayout.CENTER); jf.setSize(200,200); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Grid layout divides the container into a grid, and all components are determined according to the number of rows and columns. Each component will fill the spaces, change the size of the container, and the size of the component will also change accordingly
Construction method:
GridLayout(int rows,int columns);// Specify the number of rows and columns
GridLayout(int rows,int columns,int horizGap,int vertGap);//Specify the number of rows and columns, horizontal intervals and vertical intervals
The above is the detailed content of Three common layout methods and their uses in Java GUI. For more information, please follow other related articles on the PHP Chinese website!