GridBagLayout layout manager understanding

GridBagLayout layout

Why write this article
  • Looking at the Internet about GridBagLayout, it is said that this is a very flexible and practical layout manager, but when you check the API documentation, you will find that GridBagLayout has a total of more than a dozen parameters, which is really a headache. Reading a lot of web pages on the Internet to understand GridBagLayout, I also have a little preliminary understanding of GridBagLayout. I just want to use this first article to record my understanding of this layout manager. By the way, if you forget it in the future, you can look through it...hhh.
Some parameters of GridBagLayout
parameter Definition and understanding
gridwidth and gridheight The number of cells occupied by the grid in the horizontal (vertical) direction can use the GridBagConstraints.REMAINDER and GridBagConstraints.RELATIVE values. Usage in the example
weightx(y) The size of the parameter affects the fill weight in the horizontal and vertical directions. It is important to use with the fill parameter.
fill This parameter can be filled when your component is smaller than your area size. The possible values ​​are GridBagConstraints.NONE (no filling), GridBagConstraints.HORIZONTAL (filling only the horizontal direction), GridBagConstraints.VERTICAL (filling only the vertical direction), and GridBagConstraints.BOTH (filling in both directions).
insets Set the interval between each component. Usage new insets (int, int, int, int) The parameters are the upper, left, lower, and right intervals, the unit is px
  • Personally, I feel that only the above few can achieve a better layout. Interested friends can learn about the usage of other parameters.
Instance
First, if you want to implement the layout manager, you must first instantiate the GridBagLayout object and the GridBagConstraints method in this object
        GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

Note: In fact, this second line of code can be instantiated in multiple lines. Because if you only want this instance object c in the code, you forget to reset the constraints once you use constraints on the component later. Then it may have an impact on the following components (as for the impact, after reading the code behind, let's understand it for yourself, I don't know how to say this). But if each component instantiates a GridBagConstraints object, there is no appeal problem. Of course, the advantages and disadvantages are obvious, and the amount of code is a bit...Which way to use depends on your habit. Later, I will take only one instantiation as an example

Complete code (Understand in the comments
  • Because some understanding is better to write against the code, so justWrite in commentsUp
import java.awt.*;
import javax.swing.*;

class GUI {
    
    
	JFrame jf;
	JPanel jpanel;

	public GUI() {
    
    
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();// 实例化两个对象
		jf = new JFrame("布局演示");
		jpanel = new JPanel();
		jf.setSize(500, 300);
		jf.add(jpanel);
		jpanel.setLayout(layout);// 先把面板的布局管理器设置为前面定义实例化的layout
		c.fill = GridBagConstraints.BOTH;// 把填充状态设置填满(垂直和水平) 建议一开始设置为BOTH后面,下面的weightx(y)才能生效
		c.weightx = 3; // 虽然看着这个数字是3,但是实际的效果是:如果这一行只有这一个组件,那么按照上面的fill值,就会填满一整行,但是如果这一行有两个的话你就得看
		// 目前的这个weightx值和后面那个组件的weightx值的比值,填充比例就是按照这个比例
		c.weighty = 5;// 与上面的weightx用法一样

		addComponent("按钮1", layout, c);
		c.weightx = 2; // 例如这个地方是2,上面是3。那么运行效果就是3:2的比例填充的
		c.weighty = 5;
		c.insets = new Insets(0, 5, 0, 0);// 这个是设置第二个组件的左侧间隔5px

		c.gridwidth = GridBagConstraints.REMAINDER; // 将gridwidth的值设置为这个意思是接下来这这个组件将把这一行剩下的空间全部填充。换句话说就是一行结束的标志。
		// gridheight的用法类似
		addComponent("按钮2", layout, c);
		c.insets = new Insets(0, 0, 5, 0);// 这个是设置第三个组件的下侧间隔5px
		c.weighty = 9;
		c.gridwidth = GridBagConstraints.REMAINDER;

		addComponent("按钮3", layout, c);
		c.gridwidth = 0;// 重置
		c.weightx = 0;// 重置
		c.weighty = 4;// 与上面的那个5可以对应,比例就是5:4
		c.fill = GridBagConstraints.VERTICAL;// 只在垂直方向上面填充

		addComponent("按钮4", layout, c);
		c.weighty = 3;
		c.insets = new Insets(10, 0, 0, 0);// 这个是设置第二个组件的上侧间隔10px
		c.fill = GridBagConstraints.BOTH;

		addComponent("按钮5", layout, c);
		// 下面这一行代码建议不要写成false,因为当你放全屏的时候效果最明显
		// jf.setResizable(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);
		jf.setVisible(true);
	}

	/**
	 * 添加样式并插入的方法
	 *  String name          :这个button的名字
	 *  GridBagLayout layout :这个button的布局管理器
	 * GridBagConstraints c  :这个button的布局管理器约束条件
	 * 
	 */
	public void addComponent(String name, GridBagLayout layout, GridBagConstraints c) {
    
    
		JButton button = new JButton(name);
		layout.setConstraints(button, c);
		jpanel.add(button);
	}
}

public class Test {
    
    
	public static void main(String[] args) {
    
    
		new GUI();

	}

}

  • Example of running result
    Run example
  • Writing this kind of article for the first time, the layout and content may be a bit messy...hhh

Guess you like

Origin blog.csdn.net/snqfyyzs/article/details/109321813