【发布时间】:2019-04-26 07:34:09
【问题描述】:
所以我尝试使用GridBagLayout 制作基本的计算器布局,在浏览了一些文档后,我想出了这个:
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String args[]) {
JFrame f = new JFrame("Calculator");
f.setSize(1000, 550);
f.setResizable(false);
JPanel p1 = new JPanel();
p1.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextField out = new JTextField();
out.setColumns(15);
c.gridx = 0;
c.gridy = 0;
//c.gridwidth=4;
p1.add(out, c);
JButton b1 = new JButton("CLEAR");
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b1, c);
JButton b2 = new JButton("ERASE");
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b2, c);
JButton b3 = new JButton("ROOT");
c.gridx = 2;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b3, c);
JButton b4 = new JButton("÷");
c.gridx = 3;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b4, c);
JButton b5 = new JButton("7");
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b5, c);
JButton b6 = new JButton("8");
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b6, c);
JButton b7 = new JButton("9");
c.gridx = 2;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b7, c);
JButton b8 = new JButton("X");
c.gridx = 3;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b8, c);
JButton b9 = new JButton("4");
c.gridx = 0;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b9, c);
JButton b10 = new JButton("5");
c.gridx = 1;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b10, c);
JButton b11 = new JButton("6");
c.gridx = 2;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b11, c);
JButton b12 = new JButton("+");
c.gridx = 3;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b12, c);
JButton b13 = new JButton("1");
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b13, c);
JButton b14 = new JButton("2");
c.gridx = 1;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b14, c);
JButton b15 = new JButton("3");
c.gridx = 2;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b15, c);
JButton b16 = new JButton("-");
c.gridx = 3;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b16, c);
JButton b17 = new JButton();
c.gridx = 0;
c.gridy = 5;
c.fill = GridBagConstraints.HORIZONTAL;
//c.fill = GridBagConstraints.VERTICAL;
p1.add(b17, c);
JButton b18 = new JButton("0");
c.gridx = 1;
c.gridy = 5;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b18, c);
JButton b19 = new JButton();
c.gridx = 2;
c.gridy = 5;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b19, c);
JButton b20 = new JButton("=");
c.gridx = 3;
c.gridy = 5;
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b20, c);
f.add(p1);
f.setVisible(true);
}
}
结果如下:
所以我取消了这一行的注释以将JTextField 延长到最后:
//c.gridwidth=4;
它做了这样奇怪的事情:
我不明白为什么只显示了网格的一列。
这可能是什么原因造成的?我该怎么做才能解决这个问题?我希望 JTextField 扩展到所有列。
【问题讨论】:
标签: java swing layout awt gridbaglayout