swing布局管理器总结

FlowLayout
FlowLayout类是最简单的布局管理器。它按照和页面上排列单词的类似方式来安排组件----从左到右,直至没有多余的空间,然后转到下一行。
效果:
内容面板代码:

 

publicstaticvoid  addComponentsToPane(Container pane) {
pane.setLayout( new  FlowLayout());
pane.add( new  JButton("Button 1" ));
pane.add( new  JButton("Button 2" ));
pane.add( new  JButton("Button 3" ));
pane.add( new  JButton("Long-Named Button 4" ));
pane.add( new  JButton("5" ));
}
BorderLayout
一个BorderLayout对象将界面分成五大区域,分别用BorderLayout类的静态常量指定:
-PAGE_START
-PAGE_END
-LINE_START
-LINE_END
-CENTER
效果:
内容面板代码:

 

publicstaticvoid  addComponentsToPane(Container pane) {      
JButton button =  new  JButton("Button 1 (PAGE_START)" );
pane.add(button, BorderLayout. PAGE_START );     
button =  new  JButton("Button 2 (CENTER)" );
button.setPreferredSize( new  Dimension(200,  100));
pane.add(button, BorderLayout. CENTER );   
button =  new  JButton("Button 3 (LINE_START)" );
pane.add(button, BorderLayout. LINE_START );     
button =  new  JButton("Long-Named Button 4 (PAGE_END)" );
pane.add(button, BorderLayout. PAGE_END );      
button =  new  JButton("5 (LINE_END)" );
pane.add(button, BorderLayout. LINE_END );
}
BoxLayout
BoxLayout可以将组件由上至下或由左至右依次加入当前面板。
效果:
内容面板代码:

 

publicstaticvoid  addComponentsToPane(Container pane) {
JPanel xPanel =  new  JPanel();
xPanel.setLayout( new   BoxLayout(xPanel, BoxLayout. X_AXIS ));
addButtons(xPanel);
JPanel yPanel =  new  JPanel();
yPanel.setLayout( new   BoxLayout(yPanel, BoxLayout. Y_AXIS ));
addButtons(yPanel);
pane.add(yPanel, BorderLayout. PAGE_START );
pane.add(xPanel, BorderLayout. PAGE_END );
}
privatestaticvoid  addAButton(String text, Container container) {
JButton button =  new  JButton(text);
button.setAlignmentX(Component. CENTER_ALIGNMENT );
container.add(button);
}
privatestaticvoid  addButtons(Container container) {
addAButton("Button 1" , container);
addAButton("Button 2" , container);
addAButton("Button 3" , container);
addAButton("Long-Named Button 4" ,  container);
addAButton("5" , container);
}
CardLayout
卡片布局和其他布局不同,因为它隐藏了一些组件。卡片布局就是一组容器或者组件,它们一次仅仅显是一个,组中的每个容器称为卡片。
效果:
内容面板代码:

 

publicvoid  addComponentToPane(Container pane) {
final  JPanel contentPanel =  new  JPanel();
JPanel controlPanel =  new  JPanel();
final  CardLayout cardLayout= new  CardLayout();;
pane.setLayout( new  BorderLayout());
pane.add(contentPanel, BorderLayout. CENTER );
pane.add(controlPanel, BorderLayout. PAGE_END );
controlPanel.setLayout( new  FlowLayout());
JButton[] b =  new  JButton[10];
for  ( int  i = 0; i < 10; i++) {
b[i] =  new  JButton("No."  + i);
contentPanel.add(b[i]);
}
contentPanel.setLayout(cardLayout);
JButton nextButton =  new  JButton("next" );
nextButton.addActionListener( new   ActionListener(){
publicvoid  actionPerformed(ActionEvent  e) {
cardLayout.next(contentPanel);
}});
controlPanel.add(nextButton);
}
GridLayout
GridLayout让你建立一个组件表格,并且当组件加入时,会依序又左至右,由上至下填充到每个格子,它不能由你指定想放那个格子就放那个格子
效果:
内容面板代码:

 

publicstaticvoid  addComponentsToPane(Container pane) {
JButton[] buttons =  new  JButton[9];
pane.setLayout( new  GridLayout(3, 3));
for  ( int  i = 0; i < buttons.length ; i++) {
buttons[i] =  new  JButton(i + "" );
pane.add(buttons[i]);
}
}
GridBagLayout
GridBagLayout是所有AWT布局管理器当中最复杂的,同时他的功能也是最强大的。GridBagLayout同GridLayout一样,在容器中以网格形式来管理组件。但GridBagLayout功能要来得强大得多。
1、GridBagLayout管理的所有行和列都可以是大小不同的;
2、GridLayout把每个组件限制到一个单元格,而GridBagLayout并不这样:组件在容器中可以占据任意大小的矩形区域。
GridBagLayout通常由一个专用类来对他布局行为进行约束,该类叫GridBagConstraints。其中有11个公有成员变量,GridBagConstraints可以从这11个方面来进行控制和操纵。这些内容是:
1、gridx—组件的横向坐标;
2、girdy—组件的纵向坐标;
3、gridwidth—组件的横向宽度,也就是指组件占用的列数;
4、gridheight—组件的纵向长度,也就是指组件占用的行数;
5、weightx—指行的权重,告诉布局管理器如何分配额外的水平空间;
6、weighty—指列的权重,告诉布局管理器如何分配额外的垂直空间;
7、anchor—当组件小于其显示区域时使用此字段;
8、fill—如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充;
9、insets—指组件与表格空间四周边缘的空白区域的大小;
10、ipadx— 组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值;
11、ipady— 组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值。
说明:
1、gridx,gridy:其实就是组件行列的设置,注意都是从0开始的,比如 gridx=0,gridy=1时放在0行1列;
2、gridwidth,gridheight:默认值为1;GridBagConstraints.REMAINDER常量,代表此组件为此行或此列的最后一个组件,会占据所有剩余的空间;
3、weightx,weighty:当窗口变大时,设置各组件跟着变大的比例。比如组件A的weightx=0.5,组件B的weightx=1,那么窗口X轴变大时剩余的空间就会以1:2的比例分配给组件A和B;
4、anchor:当组件空间大于组件本身时,要将组件置于何处。 有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。
5、insets:设置组件之间彼此的间距。它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。
效果:
内容面板代码:

 

publicstaticvoid  addComponentsToPane(Container pane) {
JButton button;
pane.setLayout( new  GridBagLayout());
GridBagConstraints c =  new  GridBagConstraints();
button =  new  JButton("Button 1" );
c.fill  = GridBagConstraints. HORIZONTAL ;
c.gridx  = 0;
c.gridy  = 0;
pane.add(button, c);
button =  new  JButton("Button 2" );
c.fill  = GridBagConstraints. HORIZONTAL ;
c.weightx  = 0.5;
c.gridx  = 1;
c.gridy  = 0;
pane.add(button, c);
button =  new  JButton("Button 3" );
c.fill  = GridBagConstraints. HORIZONTAL ;
c.weightx  = 0.5;
c.gridx  = 2;
c.gridy  = 0;
pane.add(button, c);
button =  new  JButton("Long-Named Button 4" );
c.fill  = GridBagConstraints. HORIZONTAL ;
c.ipady  = 40;  // make this component tall
c.weightx  = 0.0;
c.gridwidth  = 3;
c.gridx  = 0;
c.gridy  = 1;
pane.add(button, c);
button =  new  JButton("5" );
c.fill  = GridBagConstraints. HORIZONTAL ;
c.ipady  = 0;  // reset to default
c.weighty  = 1.0;  // request any extra vertical space
c.anchor  = GridBagConstraints. PAGE_END // bottom of space
c.insets  =  new  Insets(10, 0, 0, 0);  // top padding
c.gridx  = 1;  // aligned with button 2
c.gridwidth  = 2;  // 2 columns wide
c.gridy  = 2;  // third row
pane.add(button, c);
}
一个GardBagLayout布局的左右选择框,效果:
代码为:
 
[java]  view plain copy
  1. import java.awt.Dimension;  
  2. import java.awt.GridBagConstraints;  
  3. import java.awt.GridBagLayout;  
  4. import java.awt.event.ActionEvent;  
  5. import java.awt.event.ActionListener;  
  6.   
  7. import javax.swing.BorderFactory;  
  8. import javax.swing.DefaultListModel;  
  9. import javax.swing.JButton;  
  10. import javax.swing.JFrame;  
  11. import javax.swing.JLabel;  
  12. import javax.swing.JList;  
  13. import javax.swing.JPanel;  
  14. import javax.swing.JTextField;  
  15.   
  16. public class GridBagLayoutFrame extends JFrame {  
  17.     private static final long serialVersionUID = 6819222900970457455L;  
  18.     private JPanel mainPanel = new JPanel();  
  19.     private JButton addButton = new JButton();  
  20.     private JButton leftButton = new JButton();  
  21.     private JButton rightButton = new JButton();  
  22.     private JLabel label = new JLabel();  
  23.     private JTextField field = new JTextField();  
  24.     private DefaultListModel leftModel = new DefaultListModel();  
  25.     private DefaultListModel rightMOdel = new DefaultListModel();  
  26.     private JList leftList = new JList(leftModel);  
  27.     private JList rightList = new JList(rightMOdel);  
  28.   
  29.     public GridBagLayoutFrame(String title) {  
  30.         setTitle("GridBagLayoutFrameDemo");  
  31.         setPreferredSize(new Dimension(600400));  
  32.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  33.         initComponent();  
  34.         addData();  
  35.         pack();  
  36.         setVisible(true);  
  37.     }  
  38.   
  39.     private static void createAndShowGUI() {  
  40.         new GridBagLayoutFrame("GridBagLayoutFrameDemo");  
  41.     }  
  42.   
  43.     private void initComponent() {  
  44.   
  45.         label.setText("添加选项:");  
  46.         addButton.setText("添加");  
  47.         leftList.setPreferredSize(new Dimension(150150));  
  48.         rightList.setPreferredSize(leftList.getPreferredSize());  
  49.         leftButton.setText("左");  
  50.         rightButton.setText("右");  
  51.         mainPanel.setBorder(BorderFactory.createTitledBorder("左右选择框"));  
  52.         mainPanel.setLayout(new GridBagLayout());  
  53.   
  54.         GridBagConstraints c = new GridBagConstraints();  
  55.   
  56.         c.gridx = 0// 0行0列  
  57.         c.gridy = 0;  
  58.         c.gridwidth = 1;  
  59.         c.gridheight = 1;  
  60.         c.fill = GridBagConstraints.HORIZONTAL;  
  61.         c.weightx = 0;  
  62.         c.weighty = 0;  
  63.         mainPanel.add(label, c);  
  64.   
  65.         c.gridx++;  
  66.         c.weightx = 1;  
  67.         mainPanel.add(field, c);  
  68.   
  69.         c.gridx++;  
  70.         c.weightx = 0;  
  71.         c.gridwidth = 1;  
  72.         c.gridheight = 1;  
  73.         // c.fill = GridBagConstraints.HORIZONTAL;  
  74.         mainPanel.add(addButton, c);  
  75.   
  76.         c.gridx = 0;  
  77.         c.gridy = 1;  
  78.         c.weightx = 1;  
  79.         c.weighty = 1;  
  80.         c.gridwidth = 2;  
  81.         c.gridheight = 2;  
  82.         c.fill = GridBagConstraints.BOTH;  
  83.         mainPanel.add(leftList, c);  
  84.   
  85.         c.gridx = 2;  
  86.         c.gridy = 1;  
  87.         c.gridwidth = 1;  
  88.         c.gridheight = 1;  
  89.         c.weightx = 0;  
  90.         c.weighty = 0.5;  
  91.         c.anchor = GridBagConstraints.SOUTH;  
  92.         c.fill = GridBagConstraints.HORIZONTAL;  
  93.         mainPanel.add(leftButton, c);  
  94.   
  95.         c.gridx = 2;  
  96.         c.gridy = 2;  
  97.         c.anchor = GridBagConstraints.NORTH;  
  98.         c.fill = GridBagConstraints.HORIZONTAL;  
  99.         mainPanel.add(rightButton, c);  
  100.   
  101.         c.gridx = 3;  
  102.         c.gridy = 1;  
  103.         c.gridwidth = 1;  
  104.         c.gridheight = 2;  
  105.         c.weightx = 1;  
  106.         c.weighty = 1;  
  107.         c.fill = GridBagConstraints.BOTH;  
  108.         mainPanel.add(rightList, c);  
  109.   
  110.         this.getContentPane().add(mainPanel);  
  111.     }  
  112.   
  113.     private void addData() {  
  114.         addButton.addActionListener(new ActionListener() {  
  115.             public void actionPerformed(ActionEvent e) {  
  116.                 addItem();  
  117.             }  
  118.         });  
  119.   
  120.         leftButton.addActionListener(new ActionListener() {  
  121.             public void actionPerformed(ActionEvent e) {  
  122.                 leftItem();  
  123.             }  
  124.         });  
  125.   
  126.         rightButton.addActionListener(new ActionListener() {  
  127.             public void actionPerformed(ActionEvent e) {  
  128.                 rightItem();  
  129.             }  
  130.         });  
  131.     }  
  132.   
  133.     private void addItem() {  
  134.         if (field.getText() != null && !field.getText().equals("")) {  
  135.             ((DefaultListModel) leftList.getModel())  
  136.                     .addElement(field.getText());  
  137.             field.setText("");  
  138.         }  
  139.     }  
  140.   
  141.     private void leftItem() {  
  142.         if (rightList.getSelectedIndex() != -1) {  
  143.             Object o = rightList.getSelectedValue();  
  144.             ((DefaultListModel) rightList.getModel()).remove(rightList  
  145.                     .getSelectedIndex());  
  146.             ((DefaultListModel) leftList.getModel()).addElement(o);  
  147.         }  
  148.     }  
  149.   
  150.     private void rightItem() {  
  151.         if (leftList.getSelectedIndex() != -1) {  
  152.             Object o = leftList.getSelectedValue();  
  153.             ((DefaultListModel) leftList.getModel()).remove(leftList  
  154.                     .getSelectedIndex());  
  155.             ((DefaultListModel) rightList.getModel()).addElement(o);  
  156.         }  
  157.     }  
  158.   
  159.     public static void main(String[] args) {  
  160.         javax.swing.SwingUtilities.invokeLater(new Runnable() {  
  161.             public void run() {  
  162.                 createAndShowGUI();  
  163.             }  
  164.         });  
  165.     }  
  166. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值