[英]About layouts using awt and swing
因此,我尝试使用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
扩展到所有列。
我会尝试为每个按钮添加权重。
您需要重置下一个组件的gridwidth
值,因为您正在重用相同的约束对象:
c.gridwidth=4;
p1.add(out,c);
JButton b1= new JButton("CLEAR");
c.gridx=0;
c.gridy=1;
c.gridwidth=1; // added
c.fill = GridBagConstraints.HORIZONTAL;
p1.add(b1,c);
现在,其余组件将使用1的gridwidth。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.