gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:44:19 25 4
gpt4 key购买 nike

我正在制作一款名为 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不喜欢这样,有人能给我指出正确的方向吗?

最佳答案

如果您的事件监听器拾取的事件是单击相关按钮,则无需遍历 x 和 y:

@Override
public void actionPerformed(ActionEvent e) {
Object eventSource = e.getSource();
if (eventSource instanceof JButton) {
JButton buttonClicked = (JButton) eventSource;
Color bg = buttonClicked.getBackground();
if (bg.equals(Color.white)) {
buttonClicked.setBackground(Color.yellow);
} else if (bg.equals(Color.yellow)) {
buttonClicked.setBackground(Color.white);
}
}
}

关于java - 在java中的方法中的特定索引处使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36474539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com