【发布时间】:2014-07-07 04:11:36
【问题描述】:
我一直在尝试使用摇摆元素制作多项选择题游戏,我只是使用JTextArea 来显示问题,并使用四个JButtons 来显示选项。
我希望每次用户回答问题时,通过将JButton 的背景颜色更改为正确答案来显示正确答案。
我使用的是 MVC 设计模式,因此每次用户单击选项 JButtons 时,actionPerformed 方法会调用界面中的一个方法,该方法设置 JButton 的背景颜色,使线程休眠一秒钟,然后然后它将背景设置为默认颜色。
一切似乎都是正确的,但是当运行程序时,图形界面不会改变背景颜色,尽管你可以看到线程休眠。
如果有人能帮我解决这个问题,我将不胜感激,我会附上所有描述方法的代码。
public void showCorrectAnswer (int index)
{
//JButtons array
options[index].setBackground(Color.green);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
options[index].setBackground(defaultColor);
}
选项按钮的ActionPerformed代码:
public void actionPerformed(ActionEvent e)
{
JButton source = (JButton) e.getSource();
int index =controller.getTheModel().getIndexWereTheRightAnswerIs();
controller.getTheMainView().showCorrectAnswer(index);
}
【问题讨论】:
标签: java multithreading swing