汗颜啊,昨天到今天写一个awt界面,布局差点把人恶心死,现在总算对这玩意有了一些眉目。
首先,布局大的分为2种,一种使用java awt 提供的布局来完成,这样做的好处就是在修改总体容器大小时候对整体布局影响很小,能够自动调整。另外一种就是不使用java 提供的布局管理,那么设置布局管理为null便可,但是没一个元件都需要自己定义大小位置,负责会显示不出来或者有问题,在前者布局管理器使用的是setPreferredSize设置的大小,其他都不管,后者使用的是setSize或者setBounds.
下面是我用java awt 的布局管理做的一个布局
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.SpringLayout.Constraints;
public class DsGrid extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new DsGrid("OK");
}
public DsGrid(String arg0) throws HeadlessException {
super(arg0);
this.setSize(new Dimension(600, 500));
this.createcomponent();
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void createcomponent() {
JPanel component=new JPanel();
component.setSize(this.getWidth(),this.getHeight());
component.setLayout(new BorderLayout());
JPanel searchPanel=new JPanel();
GridBagLayout searchgridbag = new GridBagLayout();
GridBagConstraints searchc = new GridBagConstraints();
searchPanel.setLayout(searchgridbag);
JPanel conditionPanel=new JPanel();
conditionPanel.setBackground(Color.red);
GridBagLayout condBagLayout = new GridBagLayout();
GridBagConstraints condConstraints = new GridBagConstraints();
conditionPanel.setLayout(condBagLayout);
Dimension lab_dimension=new Dimension(70, 30);
condConstraints.fill=GridBagConstraints.NONE;
condConstraints.weighty=0.2;
condConstraints.weightx=0.2;
condConstraints.gridx=0;
condConstraints.gridy=0;
condConstraints.gridwidth=1;
JLabel typeLable=new JLabel("Type",SwingConstants.RIGHT);
typeLable.setPreferredSize(lab_dimension);
condBagLayout.setConstraints(typeLable, condConstraints);
conditionPanel.add(typeLable);
condConstraints.gridy=1;
JLabel nameLable=new JLabel("Name",SwingConstants.RIGHT);
nameLable.setPreferredSize(lab_dimension);
condBagLayout.setConstraints(nameLable, condConstraints);
conditionPanel.add(nameLable);
condConstraints.gridy=2;
JLabel sLable=new JLabel("S..",SwingConstants.RIGHT);
sLable.setPreferredSize(lab_dimension);
condBagLayout.setConstraints(sLable, condConstraints);
conditionPanel.add(sLable);
condConstraints.gridy=3;
JLabel descLable=new JLabel("Description",SwingConstants.RIGHT);
descLable.setPreferredSize(lab_dimension);
condBagLayout.setConstraints(descLable, condConstraints);
conditionPanel.add(descLable);
condConstraints.gridy=4;
JLabel categoryLable=new JLabel("Category",SwingConstants.RIGHT);
categoryLable.setPreferredSize(lab_dimension);
condBagLayout.setConstraints(categoryLable, condConstraints);
conditionPanel.add(categoryLable);
condConstraints.gridy=0;
condConstraints.gridx=2;
condConstraints.gridwidth=4;
condConstraints.weightx=0.6;
condConstraints.fill=GridBagConstraints.HORIZONTAL;
JComboBox typeComboBox=new JComboBox();
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
Dimension query_combo_Dimension=new Dimension(120, 25);
condConstraints.gridy=1;
condConstraints.fill=GridBagConstraints.NONE;
condConstraints.anchor=GridBagConstraints.WEST;
condConstraints.gridx=2;
condConstraints.gridwidth=1;
condConstraints.weightx=0.0;
typeComboBox=new JComboBox();
typeComboBox.setPreferredSize(query_combo_Dimension);
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridy=2;
typeComboBox=new JComboBox();
typeComboBox.setPreferredSize(query_combo_Dimension);
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridy=3;
typeComboBox=new JComboBox();
typeComboBox.setPreferredSize(query_combo_Dimension);
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridy=4;
typeComboBox=new JComboBox();
typeComboBox.setPreferredSize(query_combo_Dimension);
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridx=3;
JLabel nullLab=new JLabel("");
nullLab.setPreferredSize(new Dimension(20, 20));
condBagLayout.setConstraints(nullLab, condConstraints);
conditionPanel.add(nullLab);
condConstraints.fill=GridBagConstraints.HORIZONTAL;
condConstraints.gridy=1;
condConstraints.gridx=4;
condConstraints.gridwidth=2;
typeComboBox=new JComboBox();
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridy=2;
typeComboBox=new JComboBox();
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridy=3;
typeComboBox=new JComboBox();
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
condConstraints.gridy=4;
typeComboBox=new JComboBox();
condBagLayout.setConstraints(typeComboBox, condConstraints);
conditionPanel.add(typeComboBox);
Dimension btn_Dimension=new Dimension(100, 30);
condConstraints.fill=GridBagConstraints.NONE;
condConstraints.anchor=GridBagConstraints.CENTER;
condConstraints.gridx=6;
condConstraints.gridy=0;
condConstraints.gridwidth=1;
condConstraints.weightx=0.2;
JButton searchBtn=new JButton("Search");
searchBtn.setPreferredSize(btn_Dimension);
condBagLayout.setConstraints(searchBtn, condConstraints);
conditionPanel.add(searchBtn);
condConstraints.gridy=3;
JButton selectAll=new JButton("SelectAll");
selectAll.setPreferredSize(btn_Dimension);
condBagLayout.setConstraints(selectAll, condConstraints);
conditionPanel.add(selectAll);
condConstraints.gridy=4;
JButton Dselect=new JButton("SelectAll");
Dselect.setPreferredSize(btn_Dimension);
condBagLayout.setConstraints(Dselect, condConstraints);
conditionPanel.add(Dselect);
searchc.fill = GridBagConstraints.BOTH;
searchc.gridwidth = GridBagConstraints.REMAINDER;
searchc.weightx = 1.0;
searchc.weighty=0.2;
searchgridbag.setConstraints(conditionPanel, searchc);
searchPanel.add(conditionPanel);
JPanel resultPanel=new JPanel();resultPanel.setBackground(Color.yellow);
resultPanel.setLayout(new BorderLayout());
JTable table=new JTable(1,4) ;
resultPanel.add(table,BorderLayout.CENTER);
searchc.weighty = 0.8;
searchgridbag.setConstraints(resultPanel, searchc);
searchPanel.add(resultPanel);
component.add(searchPanel,BorderLayout.CENTER);
JPanel okAndCancelPanel=new JPanel();
okAndCancelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));okAndCancelPanel.setBackground(Color.green);
okAndCancelPanel.setSize(new Dimension(component.getWidth(), 30));
okAndCancelPanel.add(new JButton("OK"));okAndCancelPanel.add(new JButton("OK"));
component.add(okAndCancelPanel,BorderLayout.SOUTH);
this.setContentPane(component);
}
}