网袋布局小结一
package com.layout.gridbag;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;/** * 不指定weightx,weghty,将控件放入容器内,只会按照组件自身的大小显示, 最重要的是放入的组件会在容器中央显示,多余的控件将会放到边缘,随便设置个值都可以避免*,当添加多个组件时,组件之间会更具权重值分配多余的空间 * * @author Administrator * */public class OfficeDemo {public JPanel createPanel() {JPanel panel = new JPanel();panel.setLayout(new GridBagLayout());GridBagConstraints constraints = new GridBagConstraints();JButton aButton = new JButton("one");JButton bButton = new JButton("two");JButton cButton = new JButton("three");JButton dButton = new JButton("four");constraints.fill = GridBagConstraints.BOTH;constraints.insets = new Insets(0, 0, 10, 10);constraints.gridx = 0;constraints.gridy = 0;constraints.weightx = 1;panel.add(aButton, constraints);// constraints.gridy = 1;constraints.gridx = 1;panel.add(bButton, constraints);constraints.gridx = 0;constraints.gridy = 1;// constraints.gridheight = 2;// constraints.weighty = 0.8;panel.add(cButton, constraints);// constraints.weighty = 0.4;constraints.gridy = 2;panel.add(dButton, constraints);return panel;}public static void main(String[] argStrings) {OfficeDemo demo = new OfficeDemo();JFrame frame = new JFrame();frame.setSize(400, 500);frame.add(demo.createPanel(), BorderLayout.CENTER);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocationRelativeTo(null);frame.setVisible(true);}}