Java中的退出计时器
在我的Java应用程序中,我有一个计时器来跟踪一个人不扫描二维码的时间。20秒后,我抛出一条消息,告诉他们使用手动输入,而不是扫描二维码。但是,一旦计时器启动,我如何停止它?例如,用户进入QR扫描面板。20秒计时器启动。用户成功扫描二维码。现在我怎样才能停止计时器?计时器在我的代码中是这样的Java中的退出计时器,java,swing,timer,passwords,Java,Swing,Timer,Passwords,在我的Java应用程序中,我有一个计时器来跟踪一个人不扫描二维码的时间。20秒后,我抛出一条消息,告诉他们使用手动输入,而不是扫描二维码。但是,一旦计时器启动,我如何停止它?例如,用户进入QR扫描面板。20秒计时器启动。用户成功扫描二维码。现在我怎样才能停止计时器?计时器在我的代码中是这样的 Timer timer = new Timer(); timer.schedule( new TimerTask(){ public void run() {
Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run() {
//delay = 20000;
// if the timer has not been stopped, proceed with timer
if(!SpectroClick.stop_timer){
//System.out.println("20 seconds later");
//scanQRPanel.getCameraView().getWebcamPanel().pause();
stopScanning();
// Create the message to display
String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
String message = "<html><div style=\"text-align: center;\">" + text + "</html>";
// Show the confirmation dialog screen, and wait for user input
int i = JOptionPane.showConfirmDialog((Component) null, message,
"Confirm", JOptionPane.OK_CANCEL_OPTION);
// User selected 'OK'
if (i == JOptionPane.OK_OPTION) {
for (SpectroClickEventListener listener : listeners) {
listener.userSelectedOverride();
}
}
// User did not select ok, go to new test
else{
startScanning();
}
}
}
}, delay);
Timer=new Timer();
timer.schedule(新TimerTask(){
public void run(){
//延迟=20000;
//如果计时器未停止,则继续使用计时器
如果(!单击。停止计时器){
//System.out.println(“20秒后”);
//scanQRPanel.getCameraView().getWebcamPanel().pause();
停止扫描();
//创建要显示的消息
String text=“未找到二维码。我们建议您使用手动覆盖。”+”
“+”按“确定”确认,或按“取消”继续扫描另一个二维码。”;
字符串消息=“文本+”;
//显示确认对话框屏幕,等待用户输入
int i=JOptionPane.showConfirmDialog((组件)null,消息,
“确认”,作业窗格。确定\取消\选项);
//用户选择“确定”
if(i==JOptionPane.OK\u选项){
for(SpectroClickEventListener侦听器:侦听器){
userSelectedOverride();
}
}
//用户未选择确定,转到新测试
否则{
开始扫描();
}
}
}
},延误);
我有一个条件来检查计时器是否正在使用,但是是否有方法向计时器抛出某种异常?问题已经得到了回答 您正在寻找的方法是“cancel()”和“purge()” 我将它们放入了您的代码中,但还没有机会运行它:
Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run() {
//delay = 20000;
// if the timer has not been stopped, proceed with timer
if(!SpectroClick.stop_timer){
//System.out.println("20 seconds later");
//scanQRPanel.getCameraView().getWebcamPanel().pause();
stopScanning();
// Create the message to display
String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
String message = "<html><div style=\"text-align: center;\">" + text + "</html>";
// Show the confirmation dialog screen, and wait for user input
int i = JOptionPane.showConfirmDialog((Component) null, message,
"Confirm", JOptionPane.OK_CANCEL_OPTION);
// User selected 'OK'
if (i == JOptionPane.OK_OPTION) {
for (SpectroClickEventListener listener : listeners) {
listener.userSelectedOverride();
}
}
// User did not select ok, go to new test
else{
startScanning();
}
} else{
timer.cancel();
timer.purge();
}
}
}, delay);
Timer=new Timer();
timer.schedule(新TimerTask(){
public void run(){
//延迟=20000;
//如果计时器未停止,则继续使用计时器
如果(!单击。停止计时器){
//System.out.println(“20秒后”);
//scanQRPanel.getCameraView().getWebcamPanel().pause();
停止扫描();
//创建要显示的消息
String text=“未找到二维码。我们建议您使用手动覆盖。”+”
“+”按“确定”确认,或按“取消”继续扫描另一个二维码。”;
字符串消息=“文本+”;
//显示确认对话框屏幕,等待用户输入
int i=JOptionPane.showConfirmDialog((组件)null,消息,
“确认”,作业窗格。确定\取消\选项);
//用户选择“确定”
if(i==JOptionPane.OK\u选项){
for(SpectroClickEventListener侦听器:侦听器){
userSelectedOverride();
}
}
//用户未选择确定,转到新测试
否则{
开始扫描();
}
}否则{
timer.cancel();
timer.purge();
}
}
},延误);
问题已经回答了
您正在寻找的方法是“cancel()”和“purge()”
我将它们放入了您的代码中,但还没有机会运行它:
Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run() {
//delay = 20000;
// if the timer has not been stopped, proceed with timer
if(!SpectroClick.stop_timer){
//System.out.println("20 seconds later");
//scanQRPanel.getCameraView().getWebcamPanel().pause();
stopScanning();
// Create the message to display
String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
String message = "<html><div style=\"text-align: center;\">" + text + "</html>";
// Show the confirmation dialog screen, and wait for user input
int i = JOptionPane.showConfirmDialog((Component) null, message,
"Confirm", JOptionPane.OK_CANCEL_OPTION);
// User selected 'OK'
if (i == JOptionPane.OK_OPTION) {
for (SpectroClickEventListener listener : listeners) {
listener.userSelectedOverride();
}
}
// User did not select ok, go to new test
else{
startScanning();
}
} else{
timer.cancel();
timer.purge();
}
}
}, delay);
Timer=new Timer();
timer.schedule(新TimerTask(){
public void run(){
//延迟=20000;
//如果计时器未停止,则继续使用计时器
如果(!单击。停止计时器){
//System.out.println(“20秒后”);
//scanQRPanel.getCameraView().getWebcamPanel().pause();
停止扫描();
//创建要显示的消息
String text=“未找到二维码。我们建议您使用手动覆盖。”+”
“+”按“确定”确认,或按“取消”继续扫描另一个二维码。”;
字符串消息=“文本+”;
//显示确认对话框屏幕,等待用户输入
int i=JOptionPane.showConfirmDialog((组件)null,消息,
“确认”,作业窗格。确定\取消\选项);
//用户选择“确定”
if(i==JOptionPane.OK\u选项){
for(SpectroClickEventListener侦听器:侦听器){
userSelectedOverride();
}
}
//用户未选择确定,转到新测试
否则{
开始扫描();
}
}否则{
timer.cancel();
timer.purge();
}
}
},延误);
您不应该为此使用java.util.Timer
,而应该使用Swing计时器,因为您当前的方法违反了Swing的单线程规则
Swing定时器可以配置为单次运行,这意味着您无需担心