【发布时间】:2015-02-26 21:33:57
【问题描述】:
我有一个按钮网格,显示在舞台上。设置和获取按钮颜色的行为很奇怪。
int X=0;
int Y=0;
buttons[X][Y].setColor(Color.BLUE); //this works, the button displays blue
boolean test = (buttons[X][Y].getColor()==Color.BLUE);
System.out.println(test); //prints 'false' , why???
System.out.println(buttons[X][Y].getColor()); //prints '0000ffff'
System.out.println(Color.BLUE); //prints '0000ffff'
我需要能够为我正在创建的游戏设置和获取按钮的颜色。有谁知道发生了什么以及我能做什么?
【问题讨论】:
-
您在比较引用而不是值。你可以改用
boolean test = buttons[X][Y].getColor.equals(Color.BLUE);。 -
谢谢 Xoppa!我非常感谢您的有益和积极的回答。
-
buttons[X][Y].getColor().equals(Color.BLUE)- 这是真正有效的。谢谢。