【发布时间】:2016-07-28 05:42:37
【问题描述】:
我正在制作一款名为 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整数传递给它) -
@khelwood 对不起,我应该更清楚,我想这样做:button[x][y].flipIt();在这种情况下,x 和 y 是我的动作侦听器的一部分,因此它们会根据我在 GUI 中按下的按钮而改变
-
如果你想在你的按钮本身上添加一个成员函数,那么你的按钮需要是你自己编写的类的实例,并且该类应该提供一个函数
flipIt()。在那个函数中,按钮是this,而x,y根本不会被引用。 -
Java 不喜欢它?像什么? :)