简体   繁体   English

jButton的添加不正确

[英]jButtons doesn't add correctly

For better reference I'm doing match3 (candy crush type) game. 为了更好的参考,我正在做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();
    }
}

and jFrame code here: 和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();
}
}

the thing is that I tried everything. 问题是我尝试了一切。 But jButtons doesn't stack according to grid. 但是jButtons不会根据网格进行堆叠。 it seems that all of them adds into one place 似乎所有人都合而为一

"But jButtons doesnt stack according to grid. it seems that all of them adds into one place" “但是jButtons并没有按照网格进行堆叠。似乎所有这些按钮都添加到了一个地方”

Since you never answered my question/comment asking what Board is, I'll make an assumption. 由于您从未回答过我的问题/评论,问过Board是谁,我将作一个假设。 (Correct me if I'm wrong) (如果我错了纠正我)

Look at your GridLayout construction 查看您的GridLayout构造

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

Assuming Board is some kind of container , let's say of dimension 300, 300. 假设Board是某种容器 ,例如尺寸300、300。

With your GridLayout constructor, you're saying that there should be 300 rows and 300 columns. 使用GridLayout构造函数时,您要说应该有300行和300列。 There's only four iterations total in both your loops, so you only have 4 buttons. 两个循环中总共只有四个迭代,因此您只有4个按钮。 The layout is mapped to 9000 available spaces. 布局映射到9000个可用空间。 So yes, all your buttons will only be placed in the top left of the grid (the first four) position, and the rest of the 8996 empty spaces will be an empty space the size of the largest button. 因此,是的,所有按钮将仅位于网格的左上方(前四个),其余8996个空白空间将是最大按钮大小的空白空间。


What you can do to see the buttons add correctly to the grid is pass a couple arguments to your drawElements() method to ensure the iterations are the same as the layout params. 可以看到将按钮正确添加到网格的方法是将几个参数传递给drawElements()方法,以确保迭代与布局参数相同。

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

Then call it like 然后像这样称呼它

public public BoardGraphics(Board board){

    int rows = ...
    int cols = ...
    setLayout(new GridLayout(rows, cols, 0, 0));
    drawElements(rows, cols);

Also you should avoid using setSize() and setLocation() for your components. 另外,您应避免将setSize()setLocation()用于组件。 You should be using layout managers and let them do the sizing and locating for you. 您应该使用布局管理器,并让他们 您进行大小调整和定位。 You can read Laying out Components Within a Container to learn more about the different layouts available. 您可以阅读在容器中布置组件,以了解更多有关可用布局的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM