GridBagLayout相对于GridLayout来说更灵活,功能也更强大,用它实现的计算器的布局效果图如下:
import java.awt.*;
import javax.swing.*;
/**
* Calculator类,实现计算器界面
* @author Ares
*/
public class Calculator{
final String[] KEYS = { "CE", "C", "←", "÷", "7", "8", "9", "×", "4","5", "6", "-", "1", "2", "3", "+", "0", ".", "=" };
JButton[] keys = new JButton[KEYS.length];
JTextField resultText = new JTextField("0");
public void addComponentsToPane(Container pane) {
GridBagLayout layout = new GridBagLayout();
pane.setLayout(layout);
resultText.setFont(new Font("Century Schoolbook", Font.PLAIN, 14));
resultText.setEditable(false);
resultText.setHorizontalAlignment(SwingConstants.RIGHT);
pane.add(resultText,new GBC(0, 0, 4, 1).setIpad(400, 50).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH));
for (int i = 0; i < keys.length; i++) {
keys[i] = new JButton(KEYS[i]);
if(i == keys.length-3){
pane.add(keys[i],new GBC(i % 4, i / 4 + 1,2,1).setIpad(0, 12).setInsets(1).setFill(GridBagConstraints.BOTH).setWeight(0.5, 0.5));
}else if (i == keys.length-2 || i == keys.length-1) {
pane.add(keys[i],new GBC(i % 4+1, i / 4 + 1).setIpad(0, 12).setInsets(1).setFill(GridBagConstraints.BOTH).setWeight(0.5, 0.5));
}else {
pane.add(keys[i], new GBC(i % 4, i / 4 + 1).setIpad(0, 12).setInsets(1).setFill(GridBagConstraints.BOTH).setWeight(0.5, 0.5));
}
}
}
public void createAndShowGUI() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().createAndShowGUI();
}
});
}
}
用来设置GridBagConstraints的类如下:
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class GBC extends GridBagConstraints {
/**
* 构造函数,用来设置组件所占单元格的坐标
*
* @param gridx 横向坐标
* @param gridy 纵向坐标
*/
public GBC(int gridx, int gridy) {
this.gridx = gridx;
this.gridy = gridy;
}
/**
* 构造函数,用来设置组件所占单元格的坐标,同时指定宽度和高度
* @param gridx 横向坐标
* @param gridy 纵向坐标
* @param gridwidth 组件宽度
* @param gridheight 组件高度
*/
public GBC(int gridx, int gridy, int gridwidth, int gridheight) {
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
}
/**
* 当组件没有空间大时,设置组件的位置
* @param anchor 组建位置
* @return 当前对象
*/
public GBC setAnchor(int anchor) {
this.anchor = anchor;
return this;
}
/**
* 设置填充
* @param fill 设置填充方式
* @return 当前对象
*/
public GBC setFill(int fill) {
this.fill = fill;
return this;
}
/**
* 设置组件随窗口延伸的幅度
* @param weightx 横向延伸大小
* @param weighty 纵向延伸大小
* @return 当前对象
*/
public GBC setWeight(double weightx, double weighty) {
this.weightx = weightx;
this.weighty = weighty;
return this;
}
/**
* 设置组件之间的间距
* @param distance 上下左右间距一样
* @return 当前对象
*/
public GBC setInsets(int distance) {
this.insets = new Insets(distance, distance, distance, distance);
return this;
}
/**
* 设置组件之间的间距
* @param top 上间距
* @param left 左间距
* @param bottom 底间距
* @param right 右间距
* @return 当前对象
*/
public GBC setInsets(int top, int left, int bottom, int right) {
this.insets = new Insets(top, left, bottom, right);
return this;
}
/**
* 设置组件内部填充空间,即给组件的最小高度或宽度添加多大的空间
* @param ipadx 横向填充
* @param ipady 纵向填充
* @return 当前对象
*/
public GBC setIpad(int ipadx, int ipady) {
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}
}