经过另一个线程的建议,我一直在玩定时器课,没有太多的快乐。下面有我的代码:Java定时器的麻烦?
public void buttonImageReveal(ActionEvent e){
Timer gameTimer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
});
String temp = e.getActionCommand();
switch(temp){
case "1":
System.out.println("case1");
((JButton)e.getSource()).setIcon(one);
gameTimer.start();
((JButton)e.getSource()).setIcon(null);
break;
我要的是在图像的一个之间有1个第二间隙被示出为图标然后该被移除。点击按钮时只发生一次。目前,我只是按下一个空白按钮?
TIA
编辑:
public void actionPerformed(ActionEvent e) {
System.out.println(e);
lastImage();
}
});
public void buttonImageReveal(ActionEvent e){
String temp = e.getActionCommand();
switch(temp){
case "1":
((JButton)e.getSource()).setIcon(one);
lastBtn = ((JButton)e.getSource());
gameTimer.start();
break;
现在做什么应该,但计时器不断去和去,你会怎么做一次你完成,并希望它停止?
+1,对于速度更快且具有更好的解释,不要忘记停止你
Timer#stop()
,或。 – camickr如果我这样做,似乎把它放入一个循环,我不明白为什么? –
@EricBanderhide你可以用更新的代码编辑你的文章吗?将其粘贴在当前代码后面以保持原始问题清晰。 –