java 使用 gridlayout 添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13787873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding buttons using gridlayout
提问by Imri Persiado
I'm trying to create a simple tic tac toe board made by 9x9 JButtons. I used a 2d array and a gridlayout but the result is nothing, a frame without any button. What I'm doing wrong?
我正在尝试创建一个由 9x9 JButtons 制作的简单井字棋盘。我使用了一个二维数组和一个网格布局,但结果什么都没有,一个没有任何按钮的框架。我做错了什么?
import java.awt.GridLayout;
import javax.swing.*;
public class Main extends JFrame
{
private JPanel panel;
private JButton[][]buttons;
private final int SIZE = 9;
private GridLayout experimentLayout;
public Main()
{
super("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setResizable(false);
setLocationRelativeTo(null);
experimentLayout = new GridLayout(SIZE,SIZE);
panel = new JPanel();
panel.setLayout(experimentLayout);
buttons = new JButton[SIZE][SIZE];
addButtons();
add(panel);
setVisible(true);
}
public void addButtons()
{
for(int k=0;k<SIZE;k++)
for(int j=0;j<SIZE;j++)
{
buttons[k][j] = new JButton(k+1+", "+(j+1));
experimentLayout.addLayoutComponent("testName", buttons[k][j]);
}
}
public static void main(String[] args)
{
new Main();
}
}
The addButton method is adding the buttons to the array and straight after to the panel.
addButton 方法将按钮添加到数组,然后直接添加到面板。
回答by Reimeus
You need to add the buttons to your JPanel
:
您需要将按钮添加到您的JPanel
:
public void addButtons(JPanel panel) {
for (int k = 0; k < SIZE; k++) {
for (int j = 0; j < SIZE; j++) {
buttons[k][j] = new JButton(k + 1 + ", " + (j + 1));
panel.add(buttons[k][j]);
}
}
}
回答by Andrew Thompson
// add buttons to the panel INSTEAD of the layout
// experimentLayout.addLayoutComponent("testName", buttons[k][j]);
panel.add(buttons[k][j]);
Further advice:
进一步建议:
- Don't extend
JFrame
, just keep a reference to one as long as needed. Only extend frame when adding or changing functionality.. - Instead of
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
usesetDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
as seen in this answer. - Instead of
setSize(500,500);
usepanel.setPreferredSize(new Dimension(500,500));
. Or better still, extendJButton
to make aSquareButton
that returns a preferred size equal to the largest preferred of width or height. The last will ensure the GUI is the size it needs to be, square & allowing enough space to show the text. - Instead of
setLocationRelativeTo(null);
usesetLocationByPlatform(true);
as seen in the answer linked in point 2. - Add
pack()
beforesetVisible(true);
which ensures the GUI is the size it needs to be, to display the content. - Instead of
setResizable(false)
callsetMinimumSize(getSize())
. - Start & update the GUI on the EDT. See Concurrency in Swingfor more details.
- 不要扩展
JFrame
,只要需要就保留对一个的引用。仅在添加或更改功能时扩展框架.. - 而不是在这个答案中看到的
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
使用。setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- 而不是
setSize(500,500);
使用panel.setPreferredSize(new Dimension(500,500));
。或者更好的是,扩展JButton
使 aSquareButton
返回的首选大小等于宽度或高度的最大首选。最后一个将确保 GUI 是它需要的大小,方形并允许足够的空间来显示文本。 - 而不是在第 2 点中链接的答案中看到的
setLocationRelativeTo(null);
使用setLocationByPlatform(true);
。 - 添加
pack()
之前setVisible(true);
确保 GUI 是显示内容所需的大小。 - 而不是
setResizable(false)
调用setMinimumSize(getSize())
。 - 在 EDT 上启动和更新 GUI。有关更多详细信息,请参阅Swing 中的并发。