Thread.sleep() 摆动定时器转换 [英] Thread.sleep() to swing Timer conversion
问题描述
我正在尝试实现一个 Thread.sleep(6000)
行,但它似乎在小程序中冻结了.当我尝试使用 Timers 时,我不确定如何使用,因为我不太擅长使用事件侦听器.在用户单击输入按钮后,我基本上尝试每 6 秒调用一次方法 fetchUrl()
.我该如何实施?
I am trying to implement a Thread.sleep(6000)
line but it seems to freeze in the applet. When I tried to use Timers, I wasn't sure how to use because I am not very good with event listeners. I am basically trying to call a method fetchUrl()
every 6 seconds, after the user clicks the enter button. How can I implement this?
public void init() {
c = getContentPane();
c.setLayout(flow);
c.setBackground(forum);
question.setForeground(Color.white);
question.setFont(tnr);
question2.setForeground(Color.white);
question2.setFont(tnr);
result.setForeground(Color.white);
result.setFont(tnr);
resp.setBorder(BorderFactory.createBevelBorder(0));
timeLength.setBorder(BorderFactory.createBevelBorder(0));
c.add(question);
c.add(resp);
c.add(question2);
c.add(timeLength);
c.add(enter);
c.add(result);
resp.requestFocus();
enter.addActionListener(this);
t = new Timer(DELAY, this);
t.setInitialDelay(DELAY);
}
public void actionPerformed(ActionEvent e) {
final String n1;
int timeMin, timeSec, count = 0, maxCount;
timeMin = Integer.parseInt(timeLength.getText());
timeSec = timeMin * 60;
maxCount = (int)(timeSec/6);
if (e.getSource() == enter) { //user clicks enter
n1 = resp.getText();
while (count < maxCount) {
fetchUrl(n1); //this method called every 6 seconds
t.start();
count++;
}
}
}
推荐答案
首先,我将分离 ActionListener
的 Timer
和 JButton代码>.
First I would start by separating the ActionListener
for the Timer
and for the JButton
.
第二,Timer 逻辑上没有发生任何事情,因为您正在通过按钮源检查吞下它.
Second nothing is happening logically with the Timer because you're swallowing it with the button source check.
第三,您应该了解计时器的工作原理.基本上对于每个 tick"(在您的情况下为六秒),计时器 ActionListener 的 actionPerformed
都会被调用.因此,如果您想要调用 fetch()
方法,那么这就是您应该在 Timer 的 actionPerformed 中可见/可访问的内容.
Third you should understand how the timer works. Basically for every "tick" (in your case six seconds) the actionPerformed
of the timer ActionListener is called. So if you want the fetch()
method called, then that's what you should be visible/accessible to the in the Timer's actionPerformed.
按钮的 ActionListener
应该只处理我认为的计时器的启动.所以只需将听众分开.给每个人一个匿名的ActionListener
,不需要让类实现ActionListener
.
The button's ActionListener
should only handle the starting of the timer I believe. So just separate the listeners. Give each one an anonymous ActionListener
and no need to make the class implement ActionListener
.
例如
timer = new Timer(DELAY, new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do some stuff every six seconds
fetchURL();
}
});
enter = new JButton(...);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
如果你想要计时器的一些自动停止功能,你可以做类似的事情
If you want some automatic stopping feature for the timer, you could do something like
timer = new Timer(DELAY, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (someStoppingCondition()) {
timer.stop();
} else {
// do some stuff every six seconds
fetchURL();
}
// do some stuff every six second
}
});
这篇关于Thread.sleep() 摆动定时器转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!