有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中的方法中使用特定索引处的数组

我正在做一个名为Light out的游戏!我想创建一个方法,在某个索引处更改按钮的颜色。为此,我使用了以下代码:

Color bg = _buttons[x][y].getBackground();
            if(bg.equals(Color.white)){
                _buttons[x][y].setBackground(Color.yellow); 
            }
            else if (bg.equals(Color.yellow)){
                _buttons[x][y].setBackground(Color.white);  

x和y是整数,它们是我看到的当前值。 基本上,我想做一个方法,无论我在什么索引。我试过了

public void flipIt(JButton _buttons[this] [this]){

            Color bg = _buttons[this][this].getBackground();


            }

但是java不喜欢这样,有人能给我指出正确的方向吗


共 (1) 个答案

  1. # 1 楼答案

    在呼叫代码中,您可以执行以下操作:

    flipIt(_buttons[x][y]);
    

    你的函数是这样的

    `public void flipIt(JButton button){
        if(button.getBackground().equals(Color.white)){
                button.setBackground(Color.yellow); 
        } else if (button.getBackground().equals(Color.yellow)){
                     button.setBackground(Color.white);
                }
     }'