首页 > 代码库 > GridBagLayout占多行效果注意

GridBagLayout占多行效果注意

 

 如果想要出现按钮2占两行的效果必须按键3、按钮4同时存在且同时可见

  • 如果缺少按钮4,则按钮2不会占两行;
  • 如果缺少按钮3、4,则按钮2也不会占两行。

 


 

技术分享  

 1 package com.wst.bj;
 2 
 3 import java.awt.GridBagConstraints;
 4 import java.awt.GridBagLayout;
 5 
 6 import javax.swing.JButton;
 7 import javax.swing.JFrame;
 8 
 9 class GridBagText {
10      
11     JFrame jframe = new JFrame("搜索");
12 
13     JButton jButton = new JButton("按钮1");
14     JButton jButton2 = new JButton("按钮2");
15     JButton jButton3 = new JButton("按钮3");
16     JButton jButton4 = new JButton("按钮4");
17 
18     public GridBagText() {
19 
20         init();
21     }
22     
23     private void init()
24     {
25         FrameUtil.initFram(jframe, 500, 700);
26         
27         GridBagLayout gridBagLayout = new GridBagLayout();
28         jframe.setLayout(gridBagLayout);
29         GridBagConstraints constraints = new  GridBagConstraints();
30         
31         constraints.fill = GridBagConstraints.BOTH;
32         constraints.weightx = 1.0;
33         constraints.weighty = 1.0;
34         
35         
36         constraints.gridheight = 1;
37         constraints.gridwidth = GridBagConstraints.REMAINDER;
38         gridBagLayout.setConstraints(jButton, constraints);
39         jframe.add(jButton);
40         
41         constraints.gridheight = 2;
42         constraints.gridwidth = 1;
43         gridBagLayout.setConstraints(jButton2, constraints);
44         jframe.add(jButton2);
45 
46         constraints.gridheight = 1;
47         constraints.gridwidth = GridBagConstraints.REMAINDER;
48         gridBagLayout.setConstraints(jButton3, constraints);
49         jframe.add(jButton3);
50         gridBagLayout.setConstraints(jButton4, constraints);
51         jframe.add(jButton4);
52         
53 
54         jframe.setSize(jframe.getPreferredSize());
55         jframe.setVisible(true);
56 //        jframe.pack();
57     }
58 }

 

 


 

  技术分享

 1 package com.wst.bj;
 2 
 3 import java.awt.GridBagConstraints;
 4 import java.awt.GridBagLayout;
 5 
 6 import javax.swing.JButton;
 7 import javax.swing.JFrame;
 8 
 9 class GridBagText {
10      
11     JFrame jframe = new JFrame("搜索");
12 
13     JButton jButton = new JButton("按钮1");
14     JButton jButton2 = new JButton("按钮2");
15     JButton jButton3 = new JButton("按钮3");
16 //    JButton jButton4 = new JButton("按钮4");
17 
18     public GridBagText() {
19 
20         init();
21     }
22     
23     private void init()
24     {
25         FrameUtil.initFram(jframe, 500, 700);
26         
27         GridBagLayout gridBagLayout = new GridBagLayout();
28         jframe.setLayout(gridBagLayout);
29         GridBagConstraints constraints = new  GridBagConstraints();
30         
31         constraints.fill = GridBagConstraints.BOTH;
32         constraints.weightx = 1.0;
33         constraints.weighty = 1.0;
34         
35         
36         constraints.gridheight = 1;
37         constraints.gridwidth = GridBagConstraints.REMAINDER;
38         gridBagLayout.setConstraints(jButton, constraints);
39         jframe.add(jButton);
40         
41         constraints.gridheight = 2;
42         constraints.gridwidth = 1;
43         gridBagLayout.setConstraints(jButton2, constraints);
44         jframe.add(jButton2);
45 
46         constraints.gridheight = 1;
47         constraints.gridwidth = GridBagConstraints.REMAINDER;
48         gridBagLayout.setConstraints(jButton3, constraints);
49         jframe.add(jButton3);
50 //        gridBagLayout.setConstraints(jButton4, constraints);
51 //        jframe.add(jButton4);
52         
53 
54         jframe.setSize(jframe.getPreferredSize());
55         jframe.setVisible(true);
56 //        jframe.pack();
57     }
58 }

 

 


 

技术分享

 1 package com.wst.bj;
 2 
 3 import java.awt.GridBagConstraints;
 4 import java.awt.GridBagLayout;
 5 
 6 import javax.swing.JButton;
 7 import javax.swing.JFrame;
 8 
 9 class GridBagText {
10      
11     JFrame jframe = new JFrame("搜索");
12 
13     JButton jButton = new JButton("按钮1");
14     JButton jButton2 = new JButton("按钮2");
15 //    JButton jButton3 = new JButton("按钮3");
16 //    JButton jButton4 = new JButton("按钮4");
17 
18     public GridBagText() {
19 
20         init();
21     }
22     
23     private void init()
24     {
25         FrameUtil.initFram(jframe, 500, 700);
26         
27         GridBagLayout gridBagLayout = new GridBagLayout();
28         jframe.setLayout(gridBagLayout);
29         GridBagConstraints constraints = new  GridBagConstraints();
30         
31         constraints.fill = GridBagConstraints.BOTH;
32         constraints.weightx = 1.0;
33         constraints.weighty = 1.0;
34         
35         
36         constraints.gridheight = 1;
37         constraints.gridwidth = GridBagConstraints.REMAINDER;
38         gridBagLayout.setConstraints(jButton, constraints);
39         jframe.add(jButton);
40         
41         constraints.gridheight = 2;
42         constraints.gridwidth = 1;
43         gridBagLayout.setConstraints(jButton2, constraints);
44         jframe.add(jButton2);
45 
46 //        constraints.gridheight = 2;
47 //        constraints.gridwidth = GridBagConstraints.REMAINDER;
48 //        gridBagLayout.setConstraints(jButton3, constraints);
49 //        jframe.add(jButton3);
50 //        gridBagLayout.setConstraints(jButton4, constraints);
51 //        jframe.add(jButton4);
52         
53 
54         jframe.setSize(jframe.getPreferredSize());
55         jframe.setVisible(true);
56 //        jframe.pack();
57     }
58 }

 

GridBagLayout占多行效果注意