将GridBagLayout调整为JPanel大小

时间:2017-04-04 09:40:42

标签: java calculator gridbaglayout

我有一个关于GridBagLayout的问题。我正在尝试制作一个简单的计算器,并且我使用GridBagLayout将所有按钮放在按钮面板中,尽管它将所有按钮放在中间而不改变按钮的大小,这是合乎逻辑的。我可以在GridLayout(将按钮的大小调整为JPanel的大小)和GridBagLayout之间得到一些东西(所以我可以按照我的意愿将它们放在宽度和顺序中)吗?

具有布局的代码如下:

...     BottomPanel(){

    this.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    backspace = new JButton("<--");
    clean = new JButton("C");
    plusminus = new JButton("+/-");
    squareroot = new JButton("\u221A");
    divide = new JButton("/");
    percent = new JButton("%");
    multiply = new JButton("*");
    fraction = new JButton("1/x");
    minus = new JButton("-");
    plus = new JButton("+");
    dot = new JButton(".");
    equals = new JButton("=");
    zero = new JButton("0");
    one = new JButton("1");
    two = new JButton("2");
    three = new JButton("3");
    four = new JButton("4");
    five = new JButton("5");
    six = new JButton("6");
    seven = new JButton("7");
    eight = new JButton("8");
    nine = new JButton("9");


    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(backspace, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(clean, gbc);

    gbc.gridx = 3;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(plusminus, gbc);

    gbc.gridx = 4;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(squareroot, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(seven, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(eight, gbc);       

    gbc.gridx = 2;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(nine, gbc);

    gbc.gridx = 3;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(divide, gbc);

    gbc.gridx = 4;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(percent, gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(four, gbc);

    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(five, gbc);

    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(six, gbc);

    gbc.gridx = 3;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(multiply, gbc);

    gbc.gridx = 4;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(fraction, gbc);

    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(one, gbc);

    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(two, gbc);

    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(three, gbc);

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(minus, gbc);

    gbc.gridx = 4;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 2;
    gbc.fill = GridBagConstraints.VERTICAL;
    this.add(equals, gbc);

    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(zero, gbc);

    gbc.gridx = 2;
    gbc.gridy = 4;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(dot, gbc);

    gbc.gridx = 3;
    gbc.gridy = 4;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    this.add(plus, gbc);




}

结果是:

Calculator

谢谢!

1 个答案:

答案 0 :(得分:0)

这至少是朝着正确方向迈出的一步:创建GridBagConstraints对象后,将其权重设置为严格大于0:

    gbc.weightx = 0.1;
    gbc.weighty = 0.1;

选择哪个值并不重要,只要大于0.0且不大于1.0。这会导致大多数按钮水平拉伸以填充可用空间并以垂直间距展开:

Small panel

Panel dragged large

您可能已经意识到,如果您希望按钮在面板显示时增长,您可以使用gbc.fill = GridBagConstraints.BOTH;