如果在java中没有按下按钮,则在5秒后关闭showOptionDialog [英] Close showOptionDialog after 5 seconds if not button is pressed in java

查看:167
本文介绍了如果在java中没有按下按钮,则在5秒后关闭showOptionDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个showOptionDialog,询问用户是否要删除某些内容。我希望关闭框架,如果5秒后没有按任何内容则不删除。我怎么能做到这一点?

I have a showOptionDialog that ask if the user wants to delete something. I want to close the frame and not delete if nothing is press after 5 seconds. How can I achive this?

这是我的代码:

         JFrame frame = new JFrame();
         frame.setAlwaysOnTop(true);
         Object[] options = {"OK"};
         int n = JOptionPane.showOptionDialog(frame,"Do you want to delete?","Title",JOptionPane.PLAIN_MESSAGE,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
         if (JOptionPane.OK_OPTION == n) {
             System.out.println("Delete");
         } else {
             System.out.println("Not Delete");
         }


推荐答案

阅读 JOptionPane API 。 JOptionPane的直接使用有一节。

Read the JOptionPane API. There is a section on "Direct Use" of the JOptionPane.

您需要手动创建选项窗格和对话框。执行此操作时,您现在将引用选项窗格使用的对话框,这意味着如果对话框仍处于打开状态,您可以处置该对话框。

You will need to create the option pane and dialog manually. When you do this you will now have a reference to the dialog used by the option pane, which means you can dispose() of the dialog if it is still open.

因此,您还需要创建一个 Swing Timer 。当计时器触发时,你使用 dialog.dispose()

So you will also need to create a Swing Timer. When the timer fires you use dialog.dispose().

你需要在对话框之前启动计时器可见。

You would need to start the Timer BEFORE the dialog is made visible.

此外,在检查选项窗格中的返回值之前,您需要停止计时器,因为您不希望计时器触发选项窗格已经关闭。

Also, before you check the return value from the option pane you would want to stop the Timer, since you don't want the Timer to fire when the option pane is already closed.

所以基本逻辑(取自API)将是:

So the basic logic (taken from the API) would be:

 JOptionPane pane = new JOptionPane(arguments);
 pane.set.Xxxx(...); // Configure
 JDialog dialog = pane.createDialog(parentComponent, title);

 Timer timer = new Timer(5000, (e) -> dialog.dispose());
 timer.start();

 dialog.setVisible(true);
 timer.stop();
 ...

编辑:


我将其更改为:

I change it to:



JOptionPane pane = new JOptionPane(
    "Do you want to delete?",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.ERROR_MESSAGE,
    null,
    options,
    options[0]); 

我上面的陈述可以看到很多问题:

I can see so many problems with the above statement:


  1. 为什么使用该构造函数?你为什么传递这么多的空参数?正如我在之前的评论中提到的,可以使用更简单的构造函数。

  1. Why are you using that constructor? Why are you passing so many null parameters? As I mentioned in my earlier comment there are simpler constructors to use.

为什么要在消息类型之前指定选项类型。你读过API了吗?你能告诉我一个有效的构造函数吗?

Why are you specifying the "option type" before the "message type". Did you read the API? Can you show me a constructor where this is valid?

3。你为什么用 JOptionPane.ERROR_MESSAGE ?这是一个问题吗?您是否应该使用 JOptionPane.QUESTION_MESSAGE

3 . Why are you using JOptionPane.ERROR_MESSAGE? This is a question? Should you not be using JOptionPane.QUESTION_MESSAGE?

您是否按照我的建议阅读了API?如果您没有阅读API并了解不同的构造函数和方法是什么,则无法编程。

Did you even read the API like I suggested? You can't program if you don't read the API and understand what the different constructors and methods are.


它返回一个整数,但在这种情况下我在哪里可以看到它?

it returns an integer, but in this case where can I see it?

根据对话框的关闭方式,它可能会也可能不会返回整数。如果单击一个按钮,它将是一个整数。如果Timer触发它将是一个字符串。

It may or may not return an integer depending on how the dialog was closed. If you click a button it will be an integer. If the Timer fires it will be a String.

再次,您是否在直接使用?它向您展示了如何从选项窗格中获取返回的值并检查它的值。

Again, did you read the section from the API on Direct Use? It shows you how to get the value returned from the option pane and check it's value.

所以我再次问,你读过API了吗?是否存在关于API中提供的代码,您不理解。如果是,请询问具体问题。

So once again I ask, did you read the API? Is there something about the code presented in the API that you don't understand. If so ask a specific question.

这篇关于如果在java中没有按下按钮,则在5秒后关闭showOptionDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆