[英]GridBagLayout fill empty space
I used GridBagLayout . 我使用了GridBagLayout 。 I can't figure out how to stretch the Jbuttons to fill the entire panel.
我不知道如何拉伸 Jbuttons来填充整个面板。
I used the following: 我使用了以下内容:
c.fill = GridBagConstraints.BOTH;
Any help would be greatly appreciated. 任何帮助将不胜感激。 I have spent much time searching for the answer but I couldn't find it.
我已经花了很多时间来寻找答案,但是找不到。 Thank so much!
非常感谢!
Below is the code for the JFrame setup. 以下是JFrame设置的代码。
public class Calculator extends JFrame {
JPanel panel;
private static JTextField output;
public Calculator() {
//...standard JFrame code....
buildButtons();
setLayout(new BorderLayout());
output = new JTextField("0");
add(output, BorderLayout.PAGE_START);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
private void buildButtons() {
panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5; // puts boxes between columns and walls.
// create button array
JButton[] button = new JButton[24];
//MC button
button[0] = new JButton("MC");
button[0].addActionListener(new randomListener());
//MR button
button[1] = new JButton("MR");
button[1].addActionListener(new randomListener());
//... the rest of the buttons...
//#3
button[22] = new JButton("3");
button[22].addActionListener(new NumberListener());
//Minus Sign
button[23] = new JButton("-");
button[23].addActionListener(new OperationListener());
int row = 0;
int col = 0;
for (JButton button1 : button) {
c.gridx = col;
c.gridy = row;
c.gridheight = 1;
c.gridwidth = 1;
c.insets = new Insets(1, 1, 1, 1);
c.fill = GridBagConstraints.BOTH;
col++;
if (col == 5) {
col = 0;
row++;
}
panel.add(button1, c);
}
//Equals Sign
JButton equalsButton = new JButton(String.valueOf("="));
equalsButton.addActionListener(new EqualsListener());
c.gridx = 4;
c.gridy = 4;
c.gridheight = 2;
c.gridwidth = 1;
c.insets = new Insets(1, 1, 1, 1);
c.fill = GridBagConstraints.BOTH;
panel.add(equalsButton, c);
//#0
JButton zeroButton = new JButton("0");
zeroButton.addActionListener(new NumberListener());
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 2;
c.gridheight = 1;
c.insets = new Insets(1, 1, 1, 1);
panel.add(zeroButton, c);
// dot
JButton doxButton = new JButton(".");
doxButton.addActionListener(new randomListener());
c.gridx = 2;
c.gridy = 5;
c.gridheight = 1;
c.gridwidth = 1;
c.insets = new Insets(1, 1, 1, 1);
c.fill = GridBagConstraints.BOTH;
panel.add(doxButton, c);
//Plus sign
JButton plusButton = new JButton(String.valueOf("+"));
plusButton.addActionListener(new OperationListener());
c.gridx = 3;
c.gridy = 5;
c.insets = new Insets(1, 1, 1, 1);
c.fill = GridBagConstraints.BOTH;
panel.add(plusButton, c);
}
// Imbedded main:
public static void main(String[] args) {
new Calculator();
}
}
设置GridBagConstraints
权重,以使按钮的尺寸在Y轴上
c.weighty = 0.5;
Use GridLayout like this: 像这样使用GridLayout:
replace yours 替换你的
panel = new JPanel(new GridBagLayout());
with 与
panel = new JPanel(new GridLayout(1, 1));
This will make your button as stretched as it is possible. 这将使您的按钮尽可能伸展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.