如何创建一个在用户闲置5分钟后弹出的对话框? (Java)的

时间:2010-10-28 15:39:11

标签: java swing time joptionpane

我有一个对话框:

JOptionPane.showMessageDialog(null,"Once medicine is given, measure temperature within 5 minutes." ,"Medication" ,JOptionPane.PLAIN_MESSAGE); 

当用户按下'ok'时,它会直接进入Jframe,要求用户使用滑块输入温度,然后按下一个按钮将其带到下一组。

无论如何,我想在用户按下'ok'后创建一些不可思议的倒计时,所以在Jframe菜单上闲置5分钟后,JFrame顶部会出现一个警告对话框并说“需要注意” ”

这让我想起了actionListener。但它将由非物理元素调用,5分钟,(不是通过任何单击按钮)。

所以也许代码应该是这样的:

JOptionPane.showMessageDialog(null,"Once medicine is given, measure temperature within 5 minutes." ,"Medication" ,JOptionPane.PLAIN_MESSAGE); 


temperature_class temp = new temperature_class(); // going to a different class where the the Jframe is coded

    if (time exceeds 5 minutes) { JOptionPane.showMessageDialog(null, "NEED attention", JOptionPane.WARNING_MESSAGE);}
    else { (do nothing) }

代码有效:

JOptionPane.showMessageDialog(null,"measure temp" ,"1" ,JOptionPane.PLAIN_MESSAGE); 

int delay = 3000; //milliseconds
 ActionListener taskPerformer = new ActionListener() {
 public void actionPerformed(ActionEvent evt) {

JOptionPane.showMessageDialog(null,"hurry." ,"Bolus Therapy Protocol" ,JOptionPane.PLAIN_MESSAGE); } };
new Timer(delay, taskPerformer).start();

temperature_class temp = new temperature_class();

但是,我希望它只做一次。那么如何调用set.Repeats(false)?

3 个答案:

答案 0 :(得分:2)

您可以将TimerTaskTimer

一起使用
class PopTask extends TimerTask {
  public void run() {
    JOptionPane.show...
  }
}

然后您要安排任务:

new Timer().schedule(new PopTask(), 1000*60*5);

也可以使用cancel()方法

取消此类计时器

答案 1 :(得分:1)

阅读How to Use Timers上的Swing教程中的部分。显示对话框时,启动计时器。关闭对话框后,停止计时器。

应该使用Swing Timer,而不是TimerTask,这样如果Timer触发,代码将在EDT上执行。

答案 2 :(得分:1)

基本上,在显示初始选项窗格后,启动Timer。 (A javax.swing一个)

您还需要一个类级变量,指示是否已输入临时值。

JOptionPane.showMessageDialog(...);
tempHasBeenEntered = false;
Timer tim = new Timer(5 * 60 * 1000, new ActionListener() { 
                 public void actionPerformed(ActionEvent e) { 
                     if (!tempHasBeenEntered)
                         JOptionPane.showMessageDialog("Hey, enter the temp!!"); 
                 } 
}
tim.setRepeats(false);
tim.start();

用户在表单中输入temp时,您需要翻转标记。