Fra*_*aly 10 java swing sleep delay thread-sleep
我制作了一个二十一点游戏,我希望AI玩家在拍卡之间暂停一下.我试过简单地使用Thread.sleep(x),但这会让它冻结,直到AI玩家完成所有的牌.我知道Swing不是线程安全的,所以我看了Timers,但我无法理解如何使用它.这是我目前的代码:
while (JB.total < 21) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Oh noes!");
}
switch (getJBTable(JB.total, JB.aces > 0)) {
case 0:
JB.hit();
break;
case 1:
break done;
case 2:
JB.hit();
JB.bet *= 2;
break done;
}
}
Run Code Online (Sandbox Code Playgroud)
BTW,hit(); 方法更新GUI.
所以我看了Timers,但我无法理解如何使用它
Timer是解决方案,因为你说你正在更新应该在EDT上完成的GUI.
我不确定你的顾虑是什么.您处理一张卡并启动计时器.当计时器启动时,您决定再拿一张卡或按住.当你按住定时器时.
那么,下面的代码显示了一个带有 JTextArea 和 JButton 的 JFrame。单击按钮时,计时器会重复将事件(在它们之间有第二个延迟)发送到与按钮相关的 actionListener,该按钮会附加一行包含当前时间的内容。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class TimerTest extends JFrame implements ActionListener{
private static final long serialVersionUID = 7416567620110237028L;
JTextArea area;
Timer timer;
int count; // Counts the number of sendings done by the timer
boolean running; // Indicates if the timer is started (true) or stopped (false)
public TimerTest() {
super("Test");
setBounds(30,30,500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
area = new JTextArea();
area.setBounds(0, 0, 500, 400);
add(area);
JButton button = new JButton("Click Me!");
button.addActionListener(this);
button.setBounds(200, 400, 100, 40);
add(button);
// Initialization of the timer. 1 second delay and this class as ActionListener
timer = new Timer(1000, this);
timer.setRepeats(true); // Send events until someone stops it
count = 0; // in the beginning, 0 events sended by timer
running = false;
System.out.println(timer.isRepeats());
setVisible(true); // Shows the frame
}
public void actionPerformed(ActionEvent e) {
if (! running) {
timer.start();
running = true;
}
// Writing the current time and increasing the cont times
area.append(Calendar.getInstance().getTime().toString()+"\n");
count++;
if (count == 10) {
timer.stop();
count = 0;
running = false;
}
}
public static void main(String[] args) {
// Executing the frame with its Timer
new TimerTest();
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,这段代码是如何使用 javax.swig.Timer 对象的示例。结合问题的具体情况。用于停止计时器的 if 语句必须更改,显然,actionPerformed 的操作也必须更改。以下片段是解决方案 actionPerformed 的框架:
public void actionPerformed(ActionEvent e) {
if (e.getComponent() == myDealerComponent()) {
// I do this if statement because the actionPerformed can treat more components
if (! running) {
timer.start();
runnig = true;
}
// Hit a card if it must be hitted
switch (getJBTable(JB.total, JB.aces > 0)) {
case 0:
JB.hit();
break;
case 1:
break done;
case 2:
JB.hit();
JB.bet *= 2;
break done;
}
if (JB.total >= 21) { // In this case we don't need count the number of times, only check the JB.total 21 reached
timer.stop()
running = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
恕我直言,这解决了问题,现在@user920769必须考虑将actionListener和启动/停止条件放在哪里......
@kleopatra:感谢您向我展示了这个计时器类的存在,我对它一无所知,它太棒了,使很多任务成为可能到一个 Swing 应用程序中:)