java - jButtons 未正确添加

标签 java swing user-interface jpanel grid-layout

为了更好的引用,我正在做 match3(糖果粉碎类型)游戏。

public class BoardGraphics extends JPanel {
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
private static final Color COLOR = new Color(0xFF0000); //0xF1E4CB

private final Board board;

public BoardGraphics(Board board){
    this.setBackground( COLOR );
    this.setSize(HEIGHT, WIDTH);
    this.setLayout(new GridLayout(board.getHeight(), board.getWidth(), 0, 0));
    this.board = board;
    setVisible(true);
    drawElements();
}
    private void drawElements(){
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            Point p = new Point(i, j);
            //Point p = new Point(1, 2);

            ImageIcon icon = null;
            try {
                BufferedImage img = null;
                img = ImageIO.read(new File(System.getProperty("user.dir") + "/assets/img/gem_" + board.getElement(p).getTileColor().toString() + ".gif"));

                icon = new ImageIcon(img);

                JButton btn = new JButton(icon);
                btn.setVisible(true);
                btn.setOpaque(false);
                btn.setContentAreaFilled(false);
                btn.setBorderPainted(false);
                btn.setSize(50, 50);
                if (i == 1) {
                    btn.setLocation(100, 100); // <- i did this for testing. with this i see 2 gems
                }
                this.add(btn);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        }
    this.revalidate();
    this.repaint();
    }
}

和 jFrame 代码在这里:

public class GameGraphics extends JFrame {
private final int WIDTH = 600;
private final int HEIGHT = 800;

private Game game = new Game();

public GameGraphics() {
    setTitle("SwitchElements");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(HEIGHT, WIDTH);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    drawBoard();
}

private void drawBoard(){
    // 0xF1E4CB
    BoardGraphics board = new BoardGraphics(game.getGameBoard());
    System.out.println((WIDTH-BoardGraphics.WIDTH)/2);
    board.setLocation( (HEIGHT-BoardGraphics.HEIGHT)/2, (WIDTH-BoardGraphics.WIDTH)/2);

    this.add( board );

    this.repaint();
}
}

问题是我已经尝试了一切。但 jButtons 不按照网格堆叠。 似乎所有这些都添加到一个地方

最佳答案

"But jButtons doesnt stack according to grid. it seems that all of them adds into one place"

由于您从未回答过我的问题/评论,询问Board是什么,所以我会做出一个假设。 (如有错误请指正)

查看您的 GridLayout 构造

new GridLayout(board.getHeight(), board.getWidth(), 0, 0)

假设 Board 是某种容器,假设尺寸为 300、300。

使用 GridLayout 构造函数,您认为应该有 300 行和 300 列。两个循环中总共只有四次迭代,因此只有 4 个按钮。该布局映射到 9000 个可用空间。所以是的,你的所有按钮只会被放置在网格的左上角(前四个)位置,其余的 8996 个空白空间将是最大按钮大小的空白空间。

<小时/>

要看到按钮正确添加到网格中,您可以做的就是向 drawElements() 方法传递几个参数,以确保迭代与布局参数相同。

private void drawElements(int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
    ....
}

然后这样调用它

public public BoardGraphics(Board board){

    int rows = ...
    int cols = ...
    setLayout(new GridLayout(rows, cols, 0, 0));
    drawElements(rows, cols);
<小时/>

此外,您还应该避免在组件中使用 setSize()setLocation()。您应该使用布局管理器,并让他们为您调整大小和定位。您可以阅读Laying out Components Within a Container了解有关可用的不同布局的更多信息。

关于java - jButtons 未正确添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041226/

相关文章:

java - 图片太大 : OutOfMemory Exception

windows - 从命令行运行 m 文件时如何隐藏 "MATLAB Command Window"?

javascript - 正则表达式 点之前/之后的第 n 个序列

java - 如何在安卓应用中显示日期选择器

java - 将 JPanel 上的组件置于前面 (Java)

Java:当没有焦点时,在第二个监视器中保持窗口全屏

java - 从数组中获取下一个 int

java - 动态创建 JOOQ 查询

用于填充 ComboBox 的字符串数组的 Java 代码

jquery - 单击 jQuery UI 对话框中的按钮之一