【发布时间】:2014-10-07 16:51:41
【问题描述】:
我的设计中有 9 个名为 box1、box2 等的文本字段。我想将其中一个设为黄色。所以我确实将文本字段的名称放在了一个数组中,并尝试使用 Random 函数来选择其中一个名称。但它不起作用。
String[] boxes = new String[]{"box1", "box2", "box3", "box4", "box5", "box6",
"box7", "box8", "box9"};
Random rand = new Random();
int randomint = rand.nextInt(9);
String thatBox = boxes[randomint];
thatBox.setBackground(Color.yellow);
【问题讨论】:
-
呃,JTextField 呢?
-
很抱歉解释不佳。我的设计中有 9 个名为 box1、box2 等的文本字段。我想将其中一个设为黄色。
-
好的,其他人回答你的问题了吗?因为我仍然很困惑
-
嗯,它看起来像答案,但它没有编译所以没有。
-
Strings没有setBackground()属性,建议你去学习
标签: java arrays random jtextfield setbackground