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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:58:42  来源:igfitidea点击:

Adding buttons using gridlayout

javaswingjpaneljbuttongrid-layout

提问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:

进一步建议:

  1. Don't extend JFrame, just keep a reference to one as long as needed. Only extend frame when adding or changing functionality..
  2. Instead of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);as seen in this answer.
  3. Instead of setSize(500,500);use panel.setPreferredSize(new Dimension(500,500));. Or better still, extend JButtonto make a SquareButtonthat 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.
  4. Instead of setLocationRelativeTo(null);use setLocationByPlatform(true);as seen in the answer linked in point 2.
  5. Add pack()before setVisible(true);which ensures the GUI is the size it needs to be, to display the content.
  6. Instead of setResizable(false)call setMinimumSize(getSize()).
  7. Start & update the GUI on the EDT. See Concurrency in Swingfor more details.
  1. 不要扩展JFrame,只要需要就保留对一个的引用。仅在添加或更改功能时扩展框架..
  2. 而不是在这个答案中看到的setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);使用。setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  3. 而不是setSize(500,500);使用panel.setPreferredSize(new Dimension(500,500));。或者更好的是,扩展JButton使 aSquareButton返回的首选大小等于宽度或高度的最大首选。最后一个将确保 GUI 是它需要的大小,方形并允许足够的空间来显示文本。
  4. 而不是在第 2 点中链接的答案中看到的setLocationRelativeTo(null);使用setLocationByPlatform(true);
  5. 添加pack()之前setVisible(true);确保 GUI 是显示内容所需的大小。
  6. 而不是setResizable(false)调用setMinimumSize(getSize())
  7. 在 EDT 上启动和更新 GUI。有关更多详细信息,请参阅Swing 中的并发