添加一个JButton的多个实例的JFrame网格 [英] Adding multiple instances of a JButton to JFrame in grid

查看:205
本文介绍了添加一个JButton的多个实例的JFrame网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下code应该创建并为特定类型的对象实例(比如颜色)的JButton我想在网格重新present。当我遍历for循环的按钮添加到JFrame它增加了什么。但是,如果我添加一个实例变量就会增加一点。任何人有一个想法?

 公共类网{保护的JButton [] []板;私人JButton的玩家;
私人的JButton openCell;
私人JButton的墙;
私人的JButton closedCell;公共电网(字串[] args){// ARGS未使用
    //实例化
    板=新的JButton [6] [6];
    布局=新的String [6] [6];    BLUECELL =的新的JButton(BLUE CELL);
    redCell =的新的JButton(红细胞);
    greenCell =的新的JButton(绿色细胞);
    whiteCell =的新的JButton(白细胞);    //配置(后添加操作)    //装饰
    blueCell.setBackground(Color.blue);
    redCell.setBackground(Color.red);
    greenCell.setBackground(Color.green);
    whiteCell.setBackground(Color.white);    对(INT行= 0;行6;;行++){
        对(INT COLS = 0; COLS 6;; COLS ++){
            如果((布局[行] [COLS])。等于('*')){
                板[行] [COLS] = BLUECELL;
            }
            否则如果((布局[行] [COLS])。等于('。')){
                板[行] [COLS] = redCell;
            }
            否则,如果((布局[行] [COLS])。等于(X)){
                板[行] [COLS] = greenCell;
            }
            其他{
                板[行] [COLS] = whiteCell;
            }
        }
    }    JFrame的游戏=新的JFrame();
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setLayout(新的GridLayout(6,6));
    game.setSize(500,500);
    的for(int i = 0; I< board.length;我++){
        对于(INT J = 0; J< board.length; J ++){
            如果((板[I] [J])。等于(BLUECELL)){
                grid.add(BLUECELL);
            }
            否则,如果((板[I] [J])。等于(redCell)){
                grid.add(redCell);
            }
            否则,如果((板[I] [J])。等于(greenCell)){
                grid.add(greenCell);
            }
                其他{
                grid.add(whiteCell);
            }
        }
    }    grid.setVisible(真);} //构造函数结束
} //网格类的结束


解决方案

您可以将组件添加到您的GUI一次。如果将其添加到其它成分,它将从previous组分被除去。你想添加相同Jbutton将几次,这是行不通的。相反,你将不得不创造更多Jbutton中。请考虑您的按钮分享哪些是允许的操作。

如果您需要更多的帮助,可以考虑张贴编译code(你目前的code不是),小的可运行,可编译程序,演示您的问题,换句话说,一个的 SSCCE


修改结果
你对此有何评论:


  

但不要这些算不算一个JButton的实例不一样的JButton? (我不明白你的答案是什么意思...)


想想看数学......多少Jbutton将你在你的code上面创建?那么,这是易图,正好是4:

  BLUECELL =的新的JButton(BLUE CELL);
redCell =的新的JButton(红细胞);
greenCell =的新的JButton(绿色细胞);
whiteCell =的新的JButton(白细胞);

所以,现在问自己,有多少Jbutton将是你想在这四个Jbutton将你的GUI显示?如果是四,那么你就可能OK(只要您使用的每个按钮),但如果它的多,那么你就麻烦了。从你的6x6格, =板JButton的新[6] [6]; ,它看起来像你想显示36 Jbutton中,如果这是真的,你'已经得到了问题。

但同样,如果还是卡住了,请考虑创建和发布的 SSCCE

The code below is supposed to create and object instance for a specific type (say color) JButton I want to represent in a grid. When I iterate through the for-loop to add the buttons to the jframe it adds nothing. But if I add a single instance variable it will add that. Anybody have an idea?

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

解决方案

You can add a component to your GUI only once. If you add it to another component, it will be removed from the previous component. You're trying to add the same JButtons several times, and that won't work. Instead you're going to have to create more JButtons. Consider having your buttons share Actions which is allowed.

If you need more help, consider posting compilable code (your current code is not), a small runnable, compilable program that demonstrates your problem, in other words, an sscce.


Edit
You comment:

But don't these count as instances of a JButton not the same JButton? (I don't understand what your answer meant...)

Think of it mathematically... how many JButtons do you create in your code above? Well, this is easy to figure, exactly 4:

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

So, now ask yourself, how many JButtons are you trying to display in your GUI with these four JButtons? If it's four, then you're possibly OK (as long as you use each button), but if it's more, then you're in trouble. From your 6x6 grid, board = new JButton [6][6];, it looks like you're trying to display 36 JButtons, and if this is true, you've got problems.

But again, if still stuck, please consider creating and posting an sscce.

这篇关于添加一个JButton的多个实例的JFrame网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆