Javas GridBagLayout 未水平或垂直填充面板

问题描述 投票:0回答:1

我将带有 GridBagLayout 的 JPanel (buttonPanel) 添加到带有 GridBagLayout 的 JFrame(GUI)。 GUI 在 BOTH 填充约束下按预期工作,而 ButtonPanel 不受 BOTH 填充约束影响。 Screenshot of what is drawn

package gui_elements;

import javax.swing.*;
import calculator.Calculator;

import java.awt.*;

public class GUI extends JFrame {

    private final int WIDTH = 400;
    private final int HEIGHT = 600;
    private Calculator calc = new Calculator();
    private String text = "";
    private OutputPanel outputPanel;
    private ButtonPanel buttonPanel;

    public GUI() {

        addPanels();
        this.setSize(WIDTH, HEIGHT);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
    }
//*****************************

    public void addPanels() {
        outputPanel = new OutputPanel("HELLLO!");
        buttonPanel = new ButtonPanel();

        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        //add and set constraints for output
        c.weighty = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        this.add(outputPanel, c);
        c.weighty = 1;
        c.gridx = 0;
        c.gridy = 1;
        this.add(buttonPanel,c);

    }
}
package gui_elements;

import javax.swing.*;
import java.awt.*;

public class ButtonPanel extends JPanel {
    JButton[] buttons = new JButton[18];


    public ButtonPanel() {
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        fillButtonArr();
        addButtons();

    }

    public void fillButtonArr() {
        buttons[0] = new JButton("(");
        buttons[1] = new JButton(")");
        buttons[2] = new JButton(" ");
        buttons[3] = new JButton("+");
        buttons[4] = new JButton("1");
        buttons[5] = new JButton("2");
        buttons[6] = new JButton("3");
        buttons[7] = new JButton("-");
        buttons[8] = new JButton("4");
        buttons[9] = new JButton("5");
        buttons[10] = new JButton("6");
        buttons[11] = new JButton("*");
        buttons[12] = new JButton("7");
        buttons[13] = new JButton("8");
        buttons[14] = new JButton("9");
        buttons[15] = new JButton("/");
        buttons[16] = new JButton("Enter");
        buttons[17] = new JButton("Del");

    }
/**********************************

    public void addButtons() {
        this.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        //operators and numbers
        for (int i = 0, row = 0; i < 16; i++) {
            if (i % 4 == 0) row++; //every four buttons, go to next row
            c.gridy = row;
            this.add(buttons[i], c);
        }

        //ENTER AND DELETE
        c.gridy = 5;
        c.gridwidth = 2;
        this.add(buttons[16], c);
        this.add(buttons[17], c);
    }


}

使用 GridBagLayout 填充面板的填充约束在外部面板上有效,但在内部面板上不起作用。

java swing jcomponent
1个回答
0
投票

在 GridBagConstraints 中,您将填充设置为两者,但仅指定 Y(垂直)的权重。将水平权重设置为 > 0,例如

c.weightx = 0.2

对于这两个组件。

© www.soinside.com 2019 - 2024. All rights reserved.