GridBagLayout 填空 space
GridBagLayout fill empty space
我使用了 GridBagLayout。我不知道如何 拉伸 Jbuttons 来填充整个面板。
我使用了以下内容:
c.fill = GridBagConstraints.BOTH;
如有任何帮助,我们将不胜感激。我花了很多时间寻找答案,但找不到。非常感谢!
下面是 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();
}
}
像这样使用 GridLayout:
替换你的
panel = new JPanel(new GridBagLayout());
和
panel = new JPanel(new GridLayout(1, 1));
这将使您的按钮尽可能地伸展。
设置 GridBagConstraints
权重,使按钮在 Y 轴上调整大小
c.weighty = 0.5;
我使用了 GridBagLayout。我不知道如何 拉伸 Jbuttons 来填充整个面板。
我使用了以下内容:
c.fill = GridBagConstraints.BOTH;
如有任何帮助,我们将不胜感激。我花了很多时间寻找答案,但找不到。非常感谢!
下面是 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();
}
}
像这样使用 GridLayout:
替换你的
panel = new JPanel(new GridBagLayout());
和
panel = new JPanel(new GridLayout(1, 1));
这将使您的按钮尽可能地伸展。
设置 GridBagConstraints
权重,使按钮在 Y 轴上调整大小
c.weighty = 0.5;