在Java中使用int或字符串添加/更改变量名称

时间:2016-06-09 18:26:16

标签: java arrays variables

我是Java的新手,所以我确信与清洁/有组织的代码相比,我的代码非常糟糕。

链接到完整代码:http://pastebin.com/UXJzU2ax

跑步时的样子。 Image

我试图通过数组中的数字在变量的末尾添加一个数字。我不确定如何实现这一目标。

我创建了一个10x10网格的按钮,其中包含随机生成的颜色,我希望在点击时更改相邻的按钮颜色,但前提是颜色相同。

颜色生成。

int[] squares;

        squares = new int[101];

        for (int i = 1; i <= 100; i++){

            squares[i] = i;
            JButton button = new JButton("" + squares[i]);


            int color = (int) (Math.random() * 4); //rand 1 to 4

            if (color == 1){

                button.setBackground(Color.red);

            } else if (color == 2){

                button.setBackground(Color.green);

            } else if (color == 3){

                button.setBackground(Color.yellow);

            } else {

                button.setBackground(Color.blue);

            } 
            button.setName("button" + (Integer.toString(squares[i])));
            button.addActionListener(new ActionListener()

            {

我知道方向的方式。

right = Integer.parseInt(button.getText()) + 1;
                        up = Integer.parseInt(button.getText()) - 10;
                        down = Integer.parseInt(button.getText()) + 10;

                        right2 = squares[right];
                        up2 = squares[up];
                        down2 = squares[down];

这是我遇到麻烦的地方。

if (btnSelectedColor.getBackground() == button.getBackground()){

                            //Where I need the change in "button", like "button[right].getBackground", or something similar.

                            if (button.getBackground() == btnSelectedColor.getBackground()){



                            }

                        }

2 个答案:

答案 0 :(得分:0)

以下是访问其他按钮的方法。

注意:我不会在这里写下所有逻辑。我会留下让邻居找你的方法。

actionPerformed回调中,你需要这样的话:

public void actionPerformed(ActionEvent e)
{
    // get all components of the panel
    Component[] comp = panel.getComponents();

    // loop through all components
    for (int i = 0; i < comp.length; i++)
    {
        // if it's a button...
        if (comp[i] instanceof JButton)
        {
            JButton b = (JButton) comp[i];

            // if b is a neighbor of button (e.g. the one clicked),
            // then change its background color
            // I leave this logic to you (seems like you wrote most if it already)

            // Hint: Convert the text of b to an int and convert the
            // text of button to an int and compare the ints to see if 
            // they are neighbors. You can convert the int of 
            // button.getText once outside of the loop. No sense in 
            // doing the conversion every time since it will be the same.
        }

    }

    // Rest of code in your actionPerformed method...

}
祝你好运!

答案 1 :(得分:0)

我会扩展JButton

  

公共类BuddyButton扩展了JButton {

新的BuddyButton将有4个字段

  私人BuddyButton北;
    私人BuddyButton南;

    私人BuddyButton东;
    私人BuddyButton west;

Initerate buttons [] []

BuddyButton buttons[][] = new BuddyButton[10][10];
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        BuddyButton buddy = new BuddyButton();
        buttons[i][j] = buddy;
        panel.add(buddy);
        buddy.addActionListener(listener);;
    }
}

循环按钮[] []再次链接所有BuddyButtons

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        BuddyButton buddy = new BuddyButton();
        buttons[i][j] = buddy;
        panel.add(buddy);
        buddy.addActionListener(listener);
    }
}

public static void linkBuddies(int i, int j) {
    if (i > 0)
        buttons[i][j].setNorth(buttons[i - 1][j]);
    if (i < 10)
        buttons[i][j].setSouth(buttons[i + 1][j]);
    if (j > 0)
        buttons[i][j].setWest(buttons[i + 1][j - 1]);
    if (j > 10)
        buttons[i][j].setEast(buttons[i + 1][j + 1]);       
}

单击BuddyButton时,将e.getSource()强制转换为BuddyButton

listener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        BuddyButton buddy = (BuddyButton) e.getSource();
        BuddyButton north = buddy.getNorth();
        BuddyButton south = buddy.getSouth();
        BuddyButton east = buddy.getEast();
        BuddyButton west = buddy.getWest();
//Check for nulls and talk to the neighbors!!!!
    }};