gpt4 book ai didi

java - 一个按钮,用于统计一分钟内被点击的次数

转载 作者:行者123 更新时间:2023-12-02 05:35:42 24 4
gpt4 key购买 nike

嘿,我正在尝试使用 swing 构建一个程序。我创建了一个按钮,我想计算一分钟内点击它的次数。但问题是,一旦我按下该按钮,它就不会被释放,并且计数器会自行增加。代码:

    while ((System.currentTimeMillis()-startTime)< 1*60*1000)
{
if(eee.getSource()==b)
{
counter++;

}
System.out.println(counter);
}

最佳答案

让我们从基础开始......

Swing 是一个单线程环境,任何阻止事件调度线程运行的行为都会阻止它更新 UI 或响应其他事件。同样,您应该只在事件调度线程的上下文中修改 UI 组件的状态。

看看Concurrency in Swing了解更多详情。

举个简单的例子,这只是允许您单击按钮,每次单击都会确定是否已经过去一分钟,如果还没有,则更新按钮的状态

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SmackMe {

public static void main(String[] args) {
new SmackMe();
}

public static final long ONE_MINUTE = 1000 * 60;

private long startTime = -1;
private int count = 0;

public SmackMe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JButton button = new JButton("Smack me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
long diff = System.currentTimeMillis() - startTime;
System.out.println((diff / 1000));
if (diff >= ONE_MINUTE) {
startTime = -1;
button.setEnabled(false);
} else {
count++;
}
button.setText(Integer.toString(count));
}
});

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

问题是它是响应式的,它需要单击按钮才能执行时间检查,更好的解决方案是有某种后台进程,可以在一分钟后禁用按钮...

这是 javax.swing.Timer 的完美用例...

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SmackMe {

public static void main(String[] args) {
new SmackMe();
}

public static final int ONE_MINUTE = 1000 * 60;

private int count = 0;
private Timer timer;
private JButton button;

public SmackMe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

button = new JButton("Smack me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!timer.isRunning()) {
timer.start();
}
count++;
button.setText(Integer.toString(count));
}
});

timer = new Timer(ONE_MINUTE, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setEnabled(false);
}
});
timer.setRepeats(false);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

看看How to use Swing Timers了解更多详情...

关于java - 一个按钮,用于统计一分钟内被点击的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com