简体   繁体   English

JOptionPane.showMessageDialog 查询?

[英]JOptionPane.showMessageDialog Query?

This is the Scenario.这是情景。

I have Code which intiates a Alram when an error is encountered.我有遇到错误时启动警报的代码。

AudioAlarm t = new AudioAlarm(song);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Awake");
    t.start();
    setRunnung(true);
    JOptionPane.showMessageDialog(null, "Alarm ...", "Alarm", JOptionPane.OK_OPTION);
    AudioAlarm.setLoop(false);
    System.out.println("Alarm Acknowledged ...");

I would like to re-design this logic in this manner,我想以这种方式重新设计这个逻辑,

If the Alarm is unacknowledged by the user over a period of time say 2 min, it turn off and message msg dialog should disappear.如果用户在一段时间内(例如 2 分钟)未确认警报,它会关闭并且消息消息对话框应该消失。

How can I Obtain this?我怎样才能获得这个?

I am able to Stop the Alram, but unable to dispose the dialog without the user pressing "OK"我可以停止 Alram,但如果用户不按“确定”,则无法处理对话框

To do what you want, you should:做你想做的事,你应该:

  • Create a JOptionPane instance using one of its constructors使用其构造函数之一创建JOptionPane实例
  • Call createDialog on this option pane to get a dialog containing this option pane在此选项窗格上调用createDialog以获取包含此选项窗格的对话框
  • Use a javax.swing.Timer instance in order to fire an action event after 2 minutes使用javax.swing.Timer实例在 2 分钟后触发动作事件
  • add an action listener to this timer which would close the dialog containing the option pane向此计时器添加一个动作侦听器,该侦听器将关闭包含选项窗格的对话框
  • show the dialog containing the option pane.显示包含选项窗格的对话框。

I do not know if what you are after can be done, but can't you instead replicate the JOptionPane as a JFrame and dispose of that one?我不知道您所追求的是否可以完成,但是您不能将 JOptionPane 复制为 JFrame 并处理掉那个吗? You can find how to close a JFrame on this Previous SO Post:您可以在Previous SO Post 上找到如何关闭 JFrame:

If you want the GUI to behave as if you clicked the "X" then you need to dispatch a windowClosing Event to the Window.如果您希望 GUI 的行为就像您单击了“X”,那么您需要向 Window 发送一个 windowClosing 事件。 The "ExitAction" from Closing An Application allows you to add this functionality to a menu item or any component that uses Actions easily.关闭应用程序的“退出操作”允许您将此功能添加到菜单项或任何使用操作的组件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM