码迷,mamicode.com
首页 > 数据库 > 详细

GridBagLayout占多行效果注意

时间:2016-12-10 22:34:49      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:height   out   bsp   awt   etc   ini   size   1.0   back   

 

 如果想要出现按钮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占多行效果注意

标签:height   out   bsp   awt   etc   ini   size   1.0   back   

原文地址:http://www.cnblogs.com/LiuYanYGZ/p/6158349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!