How to use GridBagLayout Java's most important layout manager

  • 2020-04-01 01:23:56
  • OfStack

GridBagLayout is one of the most important layout managers in Java, you can make very complex layout, it can be said that GridBagLayout is a must to learn,

The GridBagLayout class is a flexible layout manager that can align components vertically, horizontally, or along their baselines without requiring them to be the same size.

Each GridBagLayout object maintains a dynamic grid of rectangular cells, with each component occupying one or more such cells, which are called display areas.

The following is a notepad case to illustrate the use of GridBagLayout.

Analysis of the :

Instructions with arrows can be stretched.

4 takes up 4 cells, 6 takes up 4 cells. If you set 6 to stretch, then 4 will also stretch.

But if you set 4 to stretch, then the column of 7 can also be stretched, so 4 can't be stretched. We should set 4 to follow 6 to stretch.

The gray line is to get a sense of the overall layout, the number of squares occupied by the components.

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201212/2012121515394633.png ">

Runtime display

< img Alt = "" border = 0 SRC =" / / img.jbzj.com/file_images/article/201212/2012121515394634.png ">


import java.awt.*; 
import javax.swing.*; 

public class GridBagDemo extends JFrame { 
public static void main(String args[]) { 
GridBagDemo demo = new GridBagDemo(); 
} 

public GridBagDemo() { 
init(); 
this.setSize(600,600); 
this.setVisible(true); 
} 
public void init() { 
j1 = new JButton(" Open the "); 
j2 = new JButton(" save "); 
j3 = new JButton(" Save as "); 
j4 = new JPanel(); 
String[] str = { "java notes ", "C# notes ", "HTML5 notes " }; 
j5 = new JComboBox(str); 
j6 = new JTextField(); 
j7 = new JButton(" empty "); 
j8 = new JList(str); 
j9 = new JTextArea(); 
     j9.setBackground(Color.PINK);//To see the effect, I set the color
GridBagLayout layout = new GridBagLayout(); 
this.setLayout(layout); 
this.add(j1);//Add the component to the jframe
this.add(j2); 
this.add(j3); 
this.add(j4); 
this.add(j5); 
this.add(j6); 
this.add(j7); 
this.add(j8); 
this.add(j9); 
GridBagConstraints s= new GridBagConstraints();//Define a GridBagConstraints,
//Is used to control the display location of added components
s.fill = GridBagConstraints.BOTH; 
//This method sets the display if the component is in a larger area than the component itself
//NONE: does not resize the component.
//HORIZONTAL: widens a component so that it fills its display area horizontally without changing height.
//VERTICAL: heightens the component so that it fills its display area in the VERTICAL direction, but does not change the width.
//BOTH: causes the component to completely fill its display area.
s.gridwidth=1;//This method sets the number of cells occupied by the component level, which if 0 means that the component is the last in the row
s.weightx = 0;//This method sets the horizontal stretching of the component, if 0 means no stretching, if not 0 means stretching as the window grows, between 0 and 1
s.weighty=0;//This method sets the vertical stretching amplitude of the component. If it is 0, it means no stretching. If it is not 0, it will stretch as the window grows, between 0 and 1
layout.setConstraints(j1, s);//Set the component
s.gridwidth=1; 
s.weightx = 0; 
s.weighty=0; 
layout.setConstraints(j2, s); 
s.gridwidth=1; 
s.weightx = 0; 
s.weighty=0; 
layout.setConstraints(j3, s); 
s.gridwidth=0;//This method sets the number of cells occupied by the component level, which if 0 means that the component is the last in the row
s.weightx = 0;//It can't be 1, j4 is taking up 4 cells, and it can be stretched horizontally,
//But if it's 1, the lattice of the next row will also be stretched, causing the column that j7 is in to be stretched as well
//So it's going to be stretching along with j6
s.weighty=0; 
layout.setConstraints(j4, s) 
;s.gridwidth=2; 
s.weightx = 0; 
s.weighty=0; 
layout.setConstraints(j5, s); 
;s.gridwidth=4; 
s.weightx = 1; 
s.weighty=0; 
layout.setConstraints(j6, s); 
;s.gridwidth=0; 
s.weightx = 0; 
s.weighty=0; 
layout.setConstraints(j7, s); 
;s.gridwidth=2; 
s.weightx = 0; 
s.weighty=1; 
layout.setConstraints(j8, s); 
;s.gridwidth=5; 
s.weightx = 0; 
s.weighty=1; 
layout.setConstraints(j9, s); 
} 
JButton j1; 
JButton j2; 
JButton j3; 
JPanel j4; 
JComboBox j5; 
JTextField j6; 
JButton j7; 
JList j8; 
JTextArea j9; 
}

Related articles: