JButton btnNewButton = new JButton("CLICK ME!");
btnNewButton.setBounds(134, 142, 274, 77);
btnNewButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
clicked++;
String x=Integer.toString(clicked);
textArea.setText(x);
}
});
我被困在这里,我想在 GUI 中创建一个程序,计算特定时间内按钮点击的次数,例如计时器启动,然后在循环停止、按钮点击不起作用或停止计数时计算点击次数点击次数
最佳答案
有两种可能的解决方案
1.使按钮在计时器开始时可点击,在计时器停止时不可点击
或者
2.你还可以使用flag来检查计时器是否正在运行。如果计时器正在运行,则将flag设置为true,当超过时将其设置为false。像下面的片段一样
public void actionPerformed(ActionEvent e) {
if (flag) {
clicked++;
String x=Integer.toString(clicked);
textArea.setText(x);
}
else
{
// doSomething
}
}
关于java - 计算给定时间内按钮的点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607530/