【问题标题】:Autoclose JOptionPane.showconfirmDialog with a timer使用计时器自动关闭 JOptionPane.showconfirmDialog
【发布时间】:2015-05-15 05:07:59
【问题描述】:

我想创建一个带有计时器的JOptionPane.showConfirmDialog。默认选项将是退出。但是如果我点击是选项它应该继续工作,如果我点击否选项它应该退出。如果我不点击任何选项,它应该会自动退出代码。

我尝试了以下示例代码。它正在部分工作。但问题是我无法模拟是/否选项。在任何情况下,它都会从带有 YES 选项的代码中退出。

虽然这段代码取自其中一个线程,但那里的实现是不同的。我只是根据需要修改了代码。 请找到以下代码:

public class TestProgress {

public static void main(String[] args) {
    final JOptionPane msg = new JOptionPane("Database Already Exist. Do you want to continue...?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
    final JDialog dlg = msg.createDialog("Select Yes or No");
    final int n = msg.YES_NO_OPTION;
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        new Thread(new Runnable() {
          @Override
          public void run() {
            try {
              Thread.sleep(5000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }

            if(msg.YES_OPTION==n){
                System.out.println("Continue the work.. "); // should not exit
            }
            else if(msg.NO_OPTION==n)
                dlg.setVisible(false);
                System.exit(1);
            }


        }).start();
        dlg.setVisible(true);
        System.out.println("Outside code.");
    }
}  

我还需要做什么才能使其正常工作?

【问题讨论】:

  • 你的意思是this之类的?
  • 当我们讨论这个主题时,您的代码毫无意义。 JOptionPane 呈现一个模态对话框。这意味着一旦您调用setVisible,代码执行将被阻塞,直到对话框关闭。您应该检查来自JOptionPane.showMessageDialogJOptionPane.showConfirmationDialog 的结果结果,然后做出选择
  • @MadProgrammer 实际上可以将 WindowListener 或 WindowFocusListener 添加到 JOPtionPane 的对话框中以启动 Timer 并在一段时间后关闭。
  • @StanislavL 但在这种情况下,何必呢? OP 在一段时间后不会自行关闭对话框,只是“检查”值

标签: java swing timer joptionpane


【解决方案1】:

JOptionPane 的自动关闭对话框

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class TestProgress {

    public static void main(String[] args) {


        final JOptionPane msg = new JOptionPane("Database Already Exist. Do you want to continue...?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
        final JDialog dlg = msg.createDialog("Select Yes or No");
        dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dlg.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                super.componentShown(e);
                final Timer t = new Timer(5000,new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dlg.setVisible(false);
                    }
                });
                t.start();
            }
        });
        dlg.setVisible(true);
        System.out.println("Outside code.");
    }

}  

更新:

使用扩展构造函数,您可以在其中传递初始选项并指定 NO 作为默认值

JOptionPane(Object message, int messageType, int optionType,
                   Icon icon, Object[] options, Object initialValue)

【讨论】:

  • 你提到的代码不是我想要的。默认情况下它不会退出,即使我也单击“否”选项。我想要一些它会要求 YES/NO 选项的东西,单击 YES 选项后它应该继续其他工作(从 IF/ELSE 块中出来。如果我单击 NO 选项,它应该只退出那里,(系统。退出(int)。
  • 这只是一个例子。用它来添加你需要的所有逻辑。
  • 如果我在这部分添加我的代码 public void actionPerformed(ActionEvent e) { dlg.setVisible(false);它不起作用,我不知道我必须把我的逻辑放在哪里。我什至试图获取是/否按钮的值,但我也没有得到。请给我看一个上面你展示的例子,
【解决方案2】:

此问题的完整答案。

public final static boolean showConfirmDialogWithTimeout(Object params, String title, int timeout_ms) {
    final JOptionPane msg = new JOptionPane(params, JOptionPane.WARNING_MESSAGE, JOptionPane.CANCEL_OPTION);
    final JDialog dlg = msg.createDialog(title);

    msg.setInitialSelectionValue(JOptionPane.OK_OPTION);
    dlg.setAlwaysOnTop(true);
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent e) {
            super.componentShown(e);
            final Timer t = new Timer(timeout_ms, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dlg.setVisible(false);
                }

            });
            t.start();
        }
    });
    dlg.setVisible(true);

    Object selectedvalue = msg.getValue();
    if (selectedvalue.equals(JOptionPane.CANCEL_OPTION)) {
        return false;
    } else {
        return true;
    }
}

    // example usage
    String message = "The earth will explode in 10 seconds. Select CANCEL if you won't.";
    JLabel lbmsg = new JLabel(message);
    boolean result = showConfirmDialogWithTimeout(lbmsg, "Shutdown Warning", 10 * 1000);

    if (result == false) {
        Utils.showMessage(message + " cancel is selected");
    }
    else {
        Utils.showMessage(message + " timeout or okay is selected");
    }

【讨论】:

    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多