【问题标题】:Adding multiple instances of a JButton to JFrame in grid将 JButton 的多个实例添加到网格中的 JFrame
【发布时间】:2013-12-08 16:05:04
【问题描述】:

下面的代码应该为我想在网格中表示的特定类型(比如颜色)JButton 创建和对象实例。当我遍历 for 循环以将按钮添加到 jframe 时,它​​什么也没添加。但是,如果我添加一个实例变量,它将添加它。有人有想法吗?

public class Grid {

protected JButton [][] board;

private JButton player;
private JButton openCell;
private JButton wall;
private JButton closedCell;

public Grid(String [] args) { // args unused
    // Instantiation 
    board = new JButton [6][6];
    layout = new String [6][6];

    blueCell = new JButton("BLUE CELL");
    redCell = new JButton("RED CELL");
    greenCell = new JButton("GREEN CELL");
    whiteCell = new JButton("WHITE CELL");

    // Configuration (add actions later)

    // Decoration
    blueCell.setBackground(Color.blue);
    redCell.setBackground(Color.red);
    greenCell.setBackground(Color.green);
    whiteCell.setBackground(Color.white);

    for (int rows = 0; rows < 6; rows++) {
        for (int cols = 0; cols < 6; cols++) {
            if ((layout[rows][cols]).equals('*')) {
                board[rows][cols] = blueCell;
            }
            else if ((layout[rows][cols]).equals('.')) {
                board[rows][cols] = redCell;
            }
            else if ((layout[rows][cols]).equals('x')) {
                board[rows][cols] = greenCell;
            }
            else {
                board[rows][cols] = whiteCell;
            }
        }
    }

    JFrame game = new JFrame();
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setLayout(new GridLayout (6, 6));
    game.setSize(500, 500);


    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            if ((board[i][j]).equals(blueCell)) {
                grid.add(blueCell);
            }
            else if ((board[i][j]).equals(redCell)) {
                grid.add(redCell);
            }
            else if ((board[i][j]).equals(greenCell)) {
                grid.add(greenCell);
            }
                else {
                grid.add(whiteCell);
            }
        }
    }

    grid.setVisible(true);

} // end of constructor
} // end of Grid class

【问题讨论】:

  • 请不要删除您的代码和问题,因为它会使原始问题的所有答案都过时,这意味着我浪费了时间来帮助您。请恢复您之前的代码,如果您希望添加新代码作为问题的编辑,因为我已经编辑了我的答案。
  • 我已经回滚了你的问题。如果您想编辑此内容并将信息添加到底部,请执行此操作。或者,如果您的新想法完全不同,请考虑在此网站上提出一个全新的问题。

标签: java arrays swing jbutton


【解决方案1】:

您只能将一个组件添加到您的 GUI 中一次。如果将其添加到另一个组件,它将从前一个组件中删除。您尝试多次添加相同的 JButton,但这是行不通的。相反,您将不得不创建更多的 JButton。考虑让您的按钮共享允许的操作。

如果您需要更多帮助,请考虑发布可编译代码(您当前的代码不是),这是一个可运行、可编译的小型程序来演示您的问题,换句话说,sscce。


编辑
你评论:

但是这些难道不是 JButton 的实例而不是同一个 JButton 吗? (我不明白你的回答是什么意思……)

从数学上考虑...您在上面的代码中创建了多少个 JButton?嗯,这很容易计算,正好是 4:

blueCell = new JButton("BLUE CELL");
redCell = new JButton("RED CELL");
greenCell = new JButton("GREEN CELL");
whiteCell = new JButton("WHITE CELL");

那么,现在问问你自己,你想用这四个 JButton 在你的 GUI 中显示多少个 JButton?如果是四个,那么你可能没问题(只要你使用每个按钮),但如果它更多,那么你就有麻烦了。从您的 6x6 网格 board = new JButton [6][6]; 看来,您正在尝试显示 36 个 JButton,如果这是真的,那么您遇到了问题。

但同样,如果仍然卡住,请考虑创建并发布sscce。

【讨论】:

  • 但是这些不是作为 JButton 的实例而不是同一个 JButton 吗? (我不明白你的回答是什么意思......)
  • 谢谢,我会试试... this. 能解决问题吗?它可以解释很多情况,不是吗?
  • @user2449907:我不知道你说的this.&lt;cell&gt;是什么意思。但最重要的是,您需要为每个要显示的 JButton 设置一个唯一的 JButton。期间。
猜你喜欢
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 2011-09-13
相关资源
最近更新 更多