java - Java 计时器在第一轮后加速
问题描述
最近我创建了一个程序,可以测试你在五秒内点击按钮的速度,并告诉你每秒的点击率。当我只运行一次测试时一切正常,但最终我决定添加一些东西,让你在第一次运行后重新启动测试。然而,在这些随后的运行中,似乎不是从 5 递减 1,而是递减 2(Going 5, 3, 1 而不是 5, 4, 3, 2, 1)。当我对控制台进行一些输出时,似乎计时器的速度是两倍,每 0.5 秒而不是 1 秒刷新一次。我已经测试了多种方法并进行了一些研究,但似乎无法弄清楚。
public class ClickSpeedTest extends Frame implements ActionListener{
JFrame frame = new JFrame();
private boolean buttonDisabled = false;
boolean testActive = false;
boolean finished = false;
Timer t;
Label l;
Label countdown;
Button b;
Button start;
Font f = new Font("SansSerif", Font.ITALIC, 12);
Font f1 = new Font("SansSerif", Font.ITALIC, 20);
Font f2 = new Font("SansSerif", Font.BOLD, 15);
int clicks = 0;
static int interval;
static Timer timer;
public ClickSpeedTest() {
timer = new Timer();
l = new Label();
b = new Button();
countdown = new Label();
l.setBounds(10,50,400,20);
l.setName("Test");
l.setEnabled(true);
l.setText("Start clicking when ready. After 5 seconds your results will be shown.");
l.setFont(f);
countdown.setEnabled(true);
countdown.setText("5");
countdown.setBounds(200, 60, 400, 50);
countdown.setFont(f1);
b.setBounds(0, 100, 400, 350);
b.addActionListener(this);
b.setForeground(Color.MAGENTA);
b.setBackground(Color.CYAN);
b.setName("Click!");
b.setActionCommand("Click!");
b.setLabel("Click!");
add(b);add(l);add(countdown);
setName("Click Speed Test");
setSize(400,400);
setLayout(null);
setVisible(true);
setName("Click Speed Test");
}
public void actionPerformed(ActionEvent e) {
if(buttonDisabled) {
buttonDisabled = false;
this.dispose();
frame.setVisible(false);
new ClickSpeedTest();
}
if(!testActive&&!finished) {
clicks++;
testActive = true;
startTimer();
}
else if(testActive&&!finished) {
clicks++;
}
else {
buttonDisabled = true;
//System.out.println("You clicked "+clicks+" times in 5 seconds. That is approximately "+(double)clicks/5+" clicks per second.");
frame.setVisible(true);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel("You clicked "+clicks+" times in 5 seconds. That is approximately "+(double)clicks/5+" clicks per second."));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testActive = false;
finished = false;
}
//System.out.print("Click");
}
private void startTimer() {
interval = 5;
System.out.println(5);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(setInterval());
}
}, 1000, 1000);
}
private int setInterval() {
if (interval == 0) {
timer.cancel();
timer.purge();
testActive = false;
finished = true;
}
int intervalCopy = interval - 1;
if(intervalCopy == -1) {
countdown.setBounds(35, 60, 400, 50);
countdown.setFont(f2);
countdown.setText("Test Finished. See other window for results.");
b.setLabel("Click again to restart test.");
}
else {
countdown.setText(Integer.toString(intervalCopy));
}
return --interval;
}
}
主要方法:
public class Runner {
public static void main(String[]args) {
new ClickSpeedTest();
}
}
我远不是这方面的专家,我仍在学习。任何帮助表示赞赏。
解决方案
推荐阅读
- python-2.7 - 使用范围来限制列表的迭代
- leaflet - Openmaptiles - 创建自己的地图服务器与传单库集成
- matlab - Memory-speed issues when doing a scatter plot in Matlab
- javascript - php bdd 请求后更新 svg 样式
- sql - Studio 3T - SQL - IN expression
- c - User-space address at 0xfff
- wordpress - 如何在wordpress中将交换服务器设置为邮件服务器
- jsp - 将 JSP 中的 Struts 2 变量设置为子字符串
- laravel - Laravel Nova field BelongsToMany drilling down the data
- ios - AVSystemController_SystemVolumeDidChangeNotification triggers when device locked