我正在尝试使用GridBagConstraints
构建计算器。我希望等号按钮的高度是其他按钮的两倍,但它不起作用。
我的代码出了什么问题?
public class Main extends JFrame{
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bpoint,badd,bsub,bmul,bdiv,bx2,bsqrt,be,bpi,clear,bdel,plusminus,equal;
JTextField result;
public static void main(String[] args) {
new Main();
}
public Main(){
this.setSize(500, 350);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calculator");
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
GridBagConstraints theGrid = new GridBagConstraints();
theGrid.gridx = 1;
theGrid.gridy = 1;
theGrid.gridwidth = 1;
theGrid.gridheight = 1;
theGrid.weightx = 1;
theGrid.weighty = 1;
theGrid.insets = new Insets(2,2,2,2);
theGrid.anchor = GridBagConstraints.CENTER;
theGrid.fill = GridBagConstraints.BOTH;
result = new JTextField();
Font font = new Font("Tahoma", Font.PLAIN, 18);
result.setFont(font);
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
bpoint = new JButton(".");
badd = new JButton("+");
bsub = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
plusminus = new JButton("±");
equal = new JButton("=");
bx2 = new JButton("x²");
bsqrt = new JButton("√");
bdel = new JButton("←");
be = new JButton("℮");
bpi = new JButton("Π");
clear = new JButton("AC");
theGrid.gridwidth = 2;
thePanel.add(clear, theGrid);
theGrid.gridwidth = 4;
theGrid.gridx = 3;
thePanel.add(result, theGrid);
theGrid.gridwidth = 1;
theGrid.gridx = 1;
theGrid.gridy = 2;
thePanel.add(b7, theGrid);
theGrid.gridx = 2;
thePanel.add(b8, theGrid);
theGrid.gridx = 3;
thePanel.add(b9, theGrid);
theGrid.gridx = 4;
thePanel.add(badd, theGrid);
theGrid.gridx = 5;
thePanel.add(bx2, theGrid);
theGrid.gridx = 6;
thePanel.add(bdel, theGrid);
theGrid.gridy = 3;
theGrid.gridx = 1;
thePanel.add(b4, theGrid);
theGrid.gridx = 2;
thePanel.add(b5, theGrid);
theGrid.gridx = 3;
thePanel.add(b6, theGrid);
theGrid.gridx = 4;
thePanel.add(bsub, theGrid);
theGrid.gridx = 5;
thePanel.add(bsqrt, theGrid);
theGrid.gridx = 6;
thePanel.add(plusminus, theGrid);
theGrid.gridy = 4;
theGrid.gridx = 1;
thePanel.add(b1, theGrid);
theGrid.gridx = 2;
thePanel.add(b2, theGrid);
theGrid.gridx = 3;
thePanel.add(b3, theGrid);
theGrid.gridx = 4;
thePanel.add(bmul, theGrid);
theGrid.gridx = 5;
thePanel.add(be, theGrid);
theGrid.gridx = 6;
theGrid.gridheight = 2;
thePanel.add(equal, theGrid);
theGrid.gridy = 5;
theGrid.gridwidth = 2;
theGrid.gridx = 1;
thePanel.add(b0, theGrid);
theGrid.gridwidth = 1;
theGrid.gridx = 3;
thePanel.add(bpoint, theGrid);
theGrid.gridx = 4;
thePanel.add(bdiv, theGrid);
theGrid.gridx = 5;
thePanel.add(bpi, theGrid);
this.add(thePanel);
this.setResizable(false);
this.setVisible(true);
}
}
答案 0 :(得分:2)
您需要在使用后重置gridHeight
约束,以便其余按钮的高度仅为1:
theGrid.gridheight = 2;
thePanel.add(equal, theGrid);
theGrid.gridheight = 1; // added