简体   繁体   中英

Adjusting GridBagLayout to JPanel size

I have a question regarding GridBagLayout. I am trying to make a simple calculator and I put in the buttons panel all of the buttons using GridBagLayout, although it puts all of the buttons right in the middle without changing the size of the buttons, which is logical. Can I get something in between GridLayout (which adjusts the size of the buttons to the size of the JPanel) and GridBagLayout (so I can put them in width and order as I want to)?

The code with the layout is as following:

... 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);




}

and the result is:

Calculator

Thanks!

Here's at least a step in the right direction: After you have created your GridBagConstraints object, set its weights strictly greater than 0:

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

It's not important which value you pick, just greater than 0.0 and not greater than 1.0. This causes most of your buttons to stretch horizontally to fill out the available space and spread with even spacing vertically:

小面板

面板拖大

You are probably already aware that if you want the buttons to grow when the panel does, you may use gbc.fill = GridBagConstraints.BOTH; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM