首页 > 代码库 > BorderLayout布局,修改各个区域大小办法

BorderLayout布局,修改各个区域大小办法

摘自http://blog.csdn.net/zcsearching/article/details/50808446

 

BorderLayout控件大小的设置

 

使用BorderLayout时,中间的面板会随着窗体的变化而变化,其他区域的大小根据添加组件多少而变化。举个例子,现在想将一个面板固定到上边,高度为固定值,这是我们需要设置面板的高度。但是调用面板的 setSize()方法根本没有效果。调用 setMaximumSize()setMinimumSize()都没有用。

最后才发现应该调用的是 setPreferredSize()才能达到效果。

 

请看栗子。

 

 

 技术分享

 

 1 package com.wst.bj;
 2 
 3 import java.awt.BorderLayout;
 4 import java.awt.Dimension;
 5 import java.awt.Toolkit;
 6 
 7 import javax.swing.JButton;
 8 import javax.swing.JFrame;
 9 import javax.swing.JPanel;
10 import javax.swing.JTextArea;
11 import javax.swing.JTextField;
12 
13 public class LayoutDemo {
14 
15     public static void main(String[] args) {
16         new myFrame();
17     }
18 }
19 
20 /**
21  * @author liuyan
22  *
23  */
24 class myFrame {
25 
26     JFrame jframe = new JFrame("搜索");
27     JTextField textField = new JTextField(20);
28     JButton jButton = new JButton("转到");
29     JButton jButton2 = new JButton("转到2");
30     JButton jButton3 = new JButton("转到3");
31     JButton jButton4 = new JButton("转到4");
32     JTextArea textArea = new JTextArea();
33     JPanel jPanel = new JPanel(new BorderLayout(5,20));
34     JPanel jPanel2 = new JPanel(new BorderLayout());
35     JPanel jPanel3 = new JPanel(new BorderLayout());
36     JPanel jPanel4 = new JPanel(new BorderLayout());
37     
38     
39     /**
40      * 
41      */
42     public myFrame() {
43 
44         init();
45     }
46     
47     /**
48      * 
49      */
50     private void init()
51     {
52         jframe.setLayout(new BorderLayout());
53         jframe.add(jPanel2, BorderLayout.SOUTH);
54         jframe.add(jPanel3, BorderLayout.EAST);
55         jframe.add(jPanel4, BorderLayout.WEST);
56         jframe.add(jPanel, BorderLayout.NORTH);
57         
58         jPanel.add(jButton, BorderLayout.EAST);
59         jPanel.add(textField, BorderLayout.CENTER);
60 
61         jPanel2.add(jButton2);
62         jPanel3.add(jButton3);
63         jPanel4.add(jButton4);
64 
65         jPanel.setPreferredSize(new Dimension(0, 150));
66         jPanel2.setPreferredSize(new Dimension(0, 150));
67         jPanel3.setPreferredSize(new Dimension(150, 0));
68         jPanel4.setPreferredSize(new Dimension(150, 0));
69 
70         jframe.add(textArea, BorderLayout.CENTER);
71         
72         jframe.setVisible(true);
73         FrameUtil.initFram(jframe, 500, 700);
74     }
75 }
76 
77 class FrameUtil
78 {
79     public static void  initFram(JFrame f, int width, int height)
80     {
81         Toolkit toolkit = Toolkit.getDefaultToolkit();
82         f.setVisible(true);
83         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84         Dimension d = toolkit.getScreenSize();
85         int x = (int)d.getWidth();
86         int y = (int)d.getHeight();
87         f.setBounds((x-width)/2, (y - height )/2, width, height);
88     }
89 }

 


技术分享

 1 package com.wst.bj;
 2 
 3 import java.awt.BorderLayout;
 4 import java.awt.Dimension;
 5 import java.awt.Toolkit;
 6 
 7 import javax.swing.JButton;
 8 import javax.swing.JFrame;
 9 import javax.swing.JPanel;
10 import javax.swing.JTextArea;
11 import javax.swing.JTextField;
12 
13 public class LayoutDemo {
14 
15     public static void main(String[] args) {
16         new myFrame();
17     }
18 }
19 
20 /**
21  * @author liuyan
22  *
23  */
24 class myFrame {
25 
26     JFrame jframe = new JFrame("搜索");
27     JTextField textField = new JTextField(20);
28     JButton jButton = new JButton("转到");
29     JButton jButton2 = new JButton("转到2");
30     JButton jButton3 = new JButton("转到3");
31     JButton jButton4 = new JButton("转到4");
32     JTextArea textArea = new JTextArea();
33     JPanel jPanel = new JPanel(new BorderLayout(5,20));
34     JPanel jPanel2 = new JPanel(new BorderLayout());
35     JPanel jPanel3 = new JPanel(new BorderLayout());
36     JPanel jPanel4 = new JPanel(new BorderLayout());
37     
38     
39     /**
40      * 
41      */
42     public myFrame() {
43 
44         init();
45     }
46     
47     /**
48      * 
49      */
50     private void init()
51     {
52         jframe.setLayout(new BorderLayout());
53         jframe.add(jPanel2, BorderLayout.SOUTH);
54         jframe.add(jPanel3, BorderLayout.EAST);
55         jframe.add(jPanel4, BorderLayout.WEST);
56         jframe.add(jPanel, BorderLayout.NORTH);
57         
58         jPanel.add(jButton, BorderLayout.EAST);
59         jPanel.add(textField, BorderLayout.CENTER);
60 
61         jPanel2.add(jButton2);
62         jPanel3.add(jButton3);
63         jPanel4.add(jButton4);
64 
65 //        jPanel.setPreferredSize(new Dimension(0, 150));
66 //        jPanel2.setPreferredSize(new Dimension(0, 150));
67 //        jPanel3.setPreferredSize(new Dimension(150, 0));
68 //        jPanel4.setPreferredSize(new Dimension(150, 0));
69 
70         jframe.add(textArea, BorderLayout.CENTER);
71         
72         jframe.setVisible(true);
73         FrameUtil.initFram(jframe, 500, 700);
74     }
75 }
76 
77 class FrameUtil
78 {
79     public static void  initFram(JFrame f, int width, int height)
80     {
81         Toolkit toolkit = Toolkit.getDefaultToolkit();
82         f.setVisible(true);
83         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84         Dimension d = toolkit.getScreenSize();
85         int x = (int)d.getWidth();
86         int y = (int)d.getHeight();
87         f.setBounds((x-width)/2, (y - height )/2, width, height);
88     }
89 }

 

BorderLayout布局,修改各个区域大小办法