Ron*_*Ron 6 java concurrency swing timer event-dispatching
我是新手,任何帮助表示感谢.
在这段代码中,我正在翻牌,如果事实证明他们不匹配,我希望他们再次面朝下.
目前正在发生的事情:1.当点击时,第一张卡翻过来2.当点击第二张卡时,发生两件事情中的任何一件(a)如果他们是相同的他们都熬夜这是我想要的(b)如果他们是不一样的我从来没有看到第二个卡,就好象它立即重新显示卡的背面(也为我的方法定义的前面卡的背面).
我认为放入睡眠计时器可能会让第二张卡显示一段时间后再转回,但事实并非如此.
我试图使用contentPane.revalidate(); &contentPane.repaint(); 但它没有改变任何东西.
我已经输入了一些控制台输出:
Console output:
Card: 0 set
Card: 6 set
Sleeping now
Card: 6 unset
Card: 0 unset
Run Code Online (Sandbox Code Playgroud)
上面是单击两张不匹配的卡时产生的控制台输出
@Override
public void actionPerformed(ActionEvent e)
{
String buttonPressed = e.getActionCommand();
int pos = Integer.valueOf(buttonPressed);
action = Control.model.ReceiveCardsTurned(pos);
keypadArray[pos].setIcon(myIcons[pos]);
System.out.println("Card: "+pos+" set");
currentTime.setText("" + Control.model.time);
currentScore.setText("" + Control.model.score);
//contentPane.revalidate();
//contentPane.repaint();
if(Control.model.twoCardsTurned == false)
{
if (action == "unturn")
{
System.out.println("Sleeping now");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
keypadArray[pos].setIcon(back);
keypadArray[Control.model.lastCard].setIcon(back);
System.out.println("Card: "+pos+" unset");
System.out.println("Card: "+Control.model.lastCard+" unset");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您似乎缺少许多重要概念.
您采取的任何阻止EDT执行此工作的操作都会使您的应用程序看起来像是挂起的.
您不能Thread#sleep在EDT上执行任何耗时的操作(例如I/O,循环或例如),这样做会使您的应用程序"暂停",这从来都不是很好.
有关更多信息,请阅读Swing中的Concurrency.
现在,您有很多选择.您可以使用a Thread在后台"等待"并将卡片转回,或者您可以使用a SwingWorker或a javax.swing.Timer.
您遇到的另一个问题是,您不应该从ThreadEDT以外的任何UI组件更新任何UI组件.这意味着如果你使用a Thread,你将负责与EDT重新同步该线程.虽然不难,但它变得凌乱.
SwingWorker并javax.swing.Timer具有使这更容易的功能.
线程,SwingWorker非常适合执行后台处理,并且对于这个问题只是过度杀伤.相反,a javax.swing.Timer完全适合这里.
if (!Control.model.twoCardsTurned)
{
if ("unturn".equals(action))
{
new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
keypadArray[pos].setIcon(back);
keypadArray[Control.model.lastCard].setIcon(back);
System.out.println("Card: "+pos+" unset");
System.out.println("Card: "+Control.model.lastCard+" unset");
}
}).start();
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常简单的例子.您可能希望放置一些控件,以防止用户在计时器触发之前单击任何内容,例如;)
| 归档时间: |
|
| 查看次数: |
255 次 |
| 最近记录: |