Java layout Learning (2) and java layout Learning
This article introduces FlowLayout and BorderLayout. We will continue to introduce the layout method in java.
(3) GridLayout grid layout
This layout divides the entire container into a grid with M rows and * N columns.
For example:
From the model diagram, we can know this layout, which is similar to the layout of common software such as mine clearance and calculator.
There are three types of constructor for this layout.
1 GridLayout () // one row and one column 2 3 GridLayout (int rows, int cols) 4 5 GridLayout (int rows, int cols, int hgap, int vgap) // hgap horizontal and vgap vertical
When you add a control to this container, the control is added in the order of left, right, top, and bottom (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream. You cannot add controls to a specified position. Next let's look at the code
1 import java. awt. *; 2 import javax. swing. *; 3 4 class GridFrame extends JFrame 5 {6 JPanel panel = new JPanel (new GridLayout (4,4, 3,3 )); // construct the container 7 String str [] = {"7", "8", "9", "/", "4", "5 ", "6", "*", "1", "2", "3", "-", "0 ",". "," = "," + "}; 8 public GridFrame (String name) 9 {10 super (name); 11 setLayout (new BorderLayout ()); 12 JButton btnArray []; 13 btnArray = new JButton [str. length]; 14 for (int I = 0; I <str. length ; I ++) 15 {16 btnArray [I] = new JButton (str [I]); 17 panel. add (btnArray [I]); 18} 19 setVisible (true); 20 setSize (250,200); 21 setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); 22 setVisible (true); 23} 24 25 public static void main (String [] args) 26 {27 GridFrame gf = new GridFrame ("grid layout computer! "); 28} 29 30}
The display effect is as follows:
(4) GridBagLayout
GridBagLayout is also a table, but you can bind controls to containers more freely by setting rules:
For example:
Cut the container into several small Cell cells, and then add controls to these cells, at the same time in a certain (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream) concatenates several consecutive cells to form a large Cell placement control.
The procedure is as follows:
(1) set new GridBagLayout () to the specified container.
(2) GridBagConstraint gbc adds such a container rule. (Constraint indicates rules, constraints, and conditions)
The Set parameter rules are as follows:
Gridx gridy Index
The index that gridwidth gridheight spans.
Dynamic Expansion of fill
Expanded weight of weightx and weighty
(3) gb. setConstraints (control, gbc) binds controls and rules
(4) constainer. add (c) add a container and then add the control to the container.
At the same time, gbc can be reused to repeatedly set the bone size, bind rules, and add containers.
Repeat steps 2, 3, and 4
Please refer to the following code and pay attention to the addComponent () method:
1 public class NumberPad {2 private static final Insets insets = new Insets (0, 0, 0, 0); 3 public static void main (final String args []) {4 final JFrame frame = new JFrame ("NumberPad"); 5 frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // window operation 6 frame. setLayout (new GridBagLayout (); // framework layout 7 JButton button; 8 // The following uses the created class to layout the buttons 9 // The first line 10 button = new JButton ("Num"); 11 addComponent (frame, button, 0, 0, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 12 button = new JButton ("/"); 13 addComponent (frame, button, 1, 0, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 14 button = new JButton ("*"); 15 addComponent (frame, button, 2, 0, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 16 button = new JButton ("-"); 17 addComponent (frame, button, 3, 0, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 18 // 19 button = new JButton ("1"); 20 addComponent (frame, button, 0, 1, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 21 button = new JButton ("2"); 22 addComponent (frame, button, 1, 1, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 23 button = new JButton ("3"); 24 addComponent (frame, button, 2, 1, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 25 button = new JButton ("+"); 26 addComponent (frame, button, 3, 1, 1, 2, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 27 // the third row 28 button = new JButton ("4"); 29 addComponent (frame, button, 0, 2, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 30 button = new JButton ("5"); 31 addComponent (frame, button, 1, 2, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 32 button = new JButton ("6"); 33 addComponent (frame, button, 2, 2, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 34 // row 4 35 button = new JButton ("7"); 36 addComponent (frame, button, 0, 3, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 37 button = new JButton ("8"); 38 addComponent (frame, button, 1, 3, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 39 button = new JButton ("9"); 40 addComponent (frame, button, 2, 3, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 41 button = new JButton ("Enter"); 42 addComponent (frame, button, 3, 3, 1, 2, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 43 // row 44 button = new JButton ("0"); 45 addComponent (frame, button, 0, 4, 2, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 46 button = new JButton (". "); 47 addComponent (frame, button, 2, 4, 1, 1, GridBagConstraints. CENTER, GridBagConstraints. BOTH); 48 frame. setSize (250,250); 49 frame. setVisible (true); 50} 51 52 private static void addComponent (Container container, Component component, int gridx, int gridy, 53 int gridwidth, int gridheight, int anchor, int fill) {54 GridBagConstraints gbc = new GridBagConstraints (gridx, gridy, gridwidth, gridheight, 1.0, 1.0, 55 anchor, fill, insets, 0, 0); // create a mesh package object 56 iner. add (component, gbc); // add to container 57}
Ps this code is from http://blog.sina.com.cn/s/blog_6cea57330100pwvq.html
Note the following points:
(1) control the dynamic expansion of the fill parameters, there are four values: horizontal expansion, vertical expansion, horizontal vertical expansion, not expansion. Select based on your actual situation
(2) weightx, weighty is the expanded weight, this weight indicates that when the horizontal (anti-theft connection: the first from the http://www.cnblogs.com/jilodream/) or the vertical expansion of N multiples, so what is the proportion of him. Be careful here, that is, if the weight is large, if you drag the container size, the control will grow fast, and when the size changes to an hour, the weight will be reduced faster. Therefore, if you want to make the control with a large weight always large, you need to set the initial size of several controls very small, so that all controls are in a large state.