Разверните JButton в GridBagLayout - PullRequest
       59

Разверните JButton в GridBagLayout

0 голосов
/ 08 декабря 2018

Как расширить JButton в GridBagLayout?

Я попытался установить веса GridBagConstraints в ненулевые значения:

gbc.weightx = gbc.weighty = 1.0;

Но это только увеличение ширины кнопок, я хочу, чтобы кнопки тоже расширялись по вертикали.

image

Вот код, в который я добавляю кнопки в сетку:

private void initGui(){
    setTitle("Calculator");
    setSize(400,500);
    var grid = new GridBagLayout();
    setLayout(grid);
    GridBagConstraints gridCon = new GridBagConstraints();
    gridCon.weightx = gridCon.weighty = 1.0;

    gridCon.gridy = 0;
    gridCon.gridx = 0;
    gridCon.gridwidth = 4;
    gridCon.fill = gridCon.HORIZONTAL;
    add(text,gridCon);

    gridCon.gridwidth = 1 ;

    String names[] = {"+","-","/","*","="};
    for(int i = 0;i < 5; i++){
        opeButtons[i] = new JButton(names[i]);
    }

    gridCon.gridx = 3;
    for( int y = 1; y < 6; y++ ){
        gridCon.gridy = y;
        add(opeButtons[y-1],gridCon);
    }

    for(int y = 2, i = 1; y < 5; y++ ){
        for(int x = 0 ; x < 3; x++,i++) {
            gridCon.gridx = x;
            gridCon.gridy = y;
            numButtons[i] = new JButton(Integer.toString(i));
            add(numButtons[i],gridCon);
        }
    }

    gridCon.gridx = 0;
    gridCon.gridy = 1;
    add(lb,gridCon);
    gridCon.gridx = 1;
    add(rb,gridCon);
    gridCon.gridx = 2;
    add(cb,gridCon);

    numButtons[0] = new JButton("0");
    gridCon.gridx = 0;
    gridCon.gridy = 5;
    gridCon.gridwidth = 2;
    add(numButtons[0],gridCon);

    gridCon.gridwidth = 1;
    gridCon.gridx = 2;
    add(decb,gridCon);
}

1 Ответ

0 голосов
/ 08 декабря 2018

Заменить

gridCon.fill = gridCon.HORIZONTAL;

на

gridCon.fill = GridBagConstraints.BOTH;
...