SWT/JFACE和SWING/AWT代码是完全不相同的,SWT比较复杂,一旦你在事件里添加数据库操作等比较耗时的工作,就容易造成界面假死,必须用线程解决,AWT相对简单,下面是个简单例子:
public class GridBagLayoutDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 8267490385825167574L;
protected Button makebutton(String name, GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
return button;
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0; // 默认宽度
c.weighty = 0; // 默认高度
c.gridwidth = GridBagConstraints.REMAINDER; // 换行
makebutton("menu bar", gridbag, c);
c.gridwidth = 1; // 一列
c.gridheight = 2; // 竖向2行
c.weightx = 0.25; // 此列占窗口宽度25%
c.weighty = 1; // 高度占100%,指除去其它固定高度的行,剩下的100%
Button btn = makebutton("tree", gridbag, c);
btn.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(getContentPane(), "tree click");
}
});
c.gridwidth = GridBagConstraints.REMAINDER; // 换行
c.gridheight = 1; // 高度占一行
c.weightx = 0.75; // 此列占窗口宽度75%
c.weighty = 0.5; // 此列占50%,指除去其它固定高度的行,剩下的100%
makebutton("config data", gridbag, c);
makebutton("message out", gridbag, c);
c.weightx = 0; // 默认宽度
c.weighty = 0; // 默认高度
c.gridwidth = GridBagConstraints.REMAINDER; // 换行
makebutton("status bar", gridbag, c);
setSize(640, 480);
setVisible(true);
}
public static void main(String args[]) {
GridBagLayoutDemo ex1 = new GridBagLayoutDemo();
ex1.init();
}
}