jButton没有正确添加

时间:2014-04-13 09:48:52

标签: 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();
}
}
事情是我尝试了一切。但是jButton并没有根据网格进行堆叠。 似乎所有这些都加入了一个地方

1 个答案:

答案 0 :(得分: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以了解有关可用的不同布局的更多信息。