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

发布于 2024-09-30 03:22:29 字数 1363 浏览 1 评论 0原文

我有一个对话框:

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

当用户按“确定”时,它会直接进入 Jframe,要求用户使用滑块输入温度,然后按按钮进入下一组内容。

无论如何,我想在用户按下“确定”后创建某种不可见的倒计时,因此在 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) 呢?

I have a dialog box that is:

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

When the user presses 'ok', it goes straight to a Jframe that ask the user to input the temperature using a slider and then pressing a button that takes it to the next set of things.

Anyways, I want to create somekind of inivisble countdown after the user presses 'ok', so after 5 minutes of idleness on the Jframe menu, one warning dialog box should appear on top of the JFrame and says something like "NEED ATTENTION".

This reminds me of actionListener. but it will be invoked by non-physical element, 5 minutes, (not by any click of button).

So Maybe the code should be like:

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) }

Code works:

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();

However, I want it do it only once. So how do I invoke set.Repeats(false)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-10-07 03:22:29

您可以将 TimerTaskTimer 一起使用:

class PopTask extends TimerTask {
  public void run() {
    JOptionPane.show...
  }
}

然后在您想要安排任务的位置:

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

这种计时器也可以使用 cancel() 取消> 方法

You could use a TimerTask with a Timer:

class PopTask extends TimerTask {
  public void run() {
    JOptionPane.show...
  }
}

then where you want to schedule your task:

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

This kind of timers can also be canceled with cancel() method

迷途知返 2024-10-07 03:22:29

本质上,在显示初始选项窗格后,启动 <代码>计时器。 (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();

一旦用户在表单中输入临时值,您就需要翻转该标志。

Essentially, after the initial option pane is shown, start a Timer. (A javax.swing one)

You will also need a class-level variable indicating if the temp has been entered yet.

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();

You will need to flip the flag once a user enters a temp into the form.

痴意少年 2024-10-07 03:22:29

阅读 Swing 教程中有关如何使用计时器的部分。当显示对话框时,您启动计时器。当对话框关闭时,您将停止计时器。

应使用 Swing 计时器,而不是 TimerTask,以便如果计时器触发,代码将在 EDT 上执行。

Read the section from the Swing tutorial on How to Use Timers. When the dialog is displayed you start the Timer. When the dialog is closed you stop the Timer.

A Swing Timer should be used, not a TimerTask so that if the Timer fires the code will be executed on the EDT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文