将按钮放置在网格下
我试图将 2 个 JButtons 放置在 9x9 的网格下,但是当我添加按钮时,网格会更改以容纳按钮,将所有内容(包括按钮)放置在一个矩形中。
我想要的是一个网格和网格本身下方的两个按钮。
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH)); mainPanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < subPanels.length; i++)
{
for (int j = 0; j < subPanels[i].length; j++)
{
subPanels[i][j] = new JPanel(new GridLayout(3, 3));
subPanels[i][j].setBorder(
BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
mainPanel.add(subPanels[i][j]);
}
}
buttonPanel.add(clear);
buttonPanel.add(solve);
buttonPanel.setLayout(new GridLayout(1,1));
mainPanel.add(buttonPanel);
ButtonPanel 应该位于 mainPanel 下。
I am trying to position 2 JButtons under a grid of 9x9, but when I add the buttons, the grid is changed to accomodate the buttons, placing everything, including the buttons in a rectangle.
What i want is to have a grid and two buttons below the grid itself.
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
mainPanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < subPanels.length; i++)
{
for (int j = 0; j < subPanels[i].length; j++)
{
subPanels[i][j] = new JPanel(new GridLayout(3, 3));
subPanels[i][j].setBorder(
BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
mainPanel.add(subPanels[i][j]);
}
}
buttonPanel.add(clear);
buttonPanel.add(solve);
buttonPanel.setLayout(new GridLayout(1,1));
mainPanel.add(buttonPanel);
The buttonPanel is what is supposed to be under the mainPanel..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的事情可能是将 mainPanel 放入 BorderLayout 的中心,然后将 ButtonPanel 放在南边。
另外,您当前将 ButtonPanel 的 GridLayout 设置为 1 行 x 1 列。您可能应该在添加按钮之前设置布局,并将其设置为 1 行 x 2 列。
The easiest thing is probably to put your mainPanel into the center of a BorderLayout, and then put the buttonPanel in the south.
Also, you're currently setting the GridLayout for your buttonPanel to 1 row x 1 column. You should probably set the layout before adding the buttons, and set it to 1 row x 2 columns.
表单上的每个组件都有一个父组件。如果将button1放在form1上,button1.parent将为“form1”。
将所需按钮的父级更改为适当的父级
Every component on a form has a parent. If you place button1 on form1, button1.parent will be 'form1'.
change the parent of the buttons you want to the appropriate parent