jButtons无法正确添加

问题描述 投票:1回答:1

为了更好地参考我正在做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();
}
}

问题是我尝试了一切。但是jButton根据网格没有堆叠。似乎所有这些都加入了一个地方

java swing user-interface jpanel grid-layout
1个回答
1
投票

“但是jButton没有根据网格堆叠。似乎所有这些都加入了一个地方”

既然你从来没有回答我的问题/评论询问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以了解有关不同布局的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.