Java:显示消息对话框 10 秒并删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6530157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java:Show message dialog for 10sec and remove?
提问by Harinder
Hi i want to show JOptionPane.showMessageDialog(null, "Appication already running");
嗨,我想展示 JOptionPane.showMessageDialog(null, "Appication already running");
for 10sec and then remove it.how can i dothat?
10 秒,然后删除它。我该怎么做?
回答by Denis Tulskiy
You can create a JOptionPane manually, without static methods:
您可以手动创建 JOptionPane,无需静态方法:
JOptionPane pane = new JOptionPane("Your message", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(parent, "Title");
then you can show the dialog and fire up a timer to hide it after ten seconds.
然后您可以显示对话框并启动计时器以在十秒后隐藏它。
回答by AndriesH
I tried these answers and ran into the problem that showing the dialog is a blocking call, so a timer can't work. The following gets around this problem.
我尝试了这些答案并遇到了显示对话框是阻塞调用的问题,因此计时器无法工作。下面解决了这个问题。
JOptionPane opt = new JOptionPane("Application already running", JOptionPane.WARNING_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}); // no buttons
final JDialog dlg = opt.createDialog("Error");
new Thread(new Runnable()
{
public void run()
{
try
{
Thread.sleep(10000);
dlg.dispose();
}
catch ( Throwable th )
{
tracea("setValidComboIndex(): error :\n"
+ cThrowable.getStackTrace(th));
}
}
}).start();
dlg.setVisible(true);
回答by Jake
My Java is a bit rusty but you should be able to just use the standard Timer
class:
我的 Java 有点生疏,但您应该可以只使用标准Timer
类:
import java.util.Timer;
int timeout_ms = 10000;//10 * 1000
Timer timer = new Timer();
timer.schedule(new CloseDialogTask(), timeout_ms);
//implement your CloseDialogTask:
class CloseDialogTask extends TimerTask {
public void run() {
//close dialog code goes here
}
}
回答by RRICK88
// =====================
// yes = 0, no = 1, cancel = 2
// timer = uninitializedValue, [x] = null
public static void DialogBox() {
JOptionPane MsgBox = new JOptionPane("Continue?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
JDialog dlg = MsgBox.createDialog("Select Yes or No");
dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dlg.addComponentListener(new ComponentAdapter() {
@Override
// =====================
public void componentShown(ComponentEvent e) {
// =====================
super.componentShown(e);
Timer t; t = new Timer(7000, new ActionListener() {
@Override
// =====================
public void actionPerformed(ActionEvent e) {
// =====================
dlg.setVisible(false);
}
});
t.setRepeats(false);
t.start();
}
});
dlg.setVisible(true);
Object n = MsgBox.getValue();
System.out.println(n);
System.out.println("Finished");
dlg.dispose();
}
}
回答by Nirmal- thInk beYond
make small frame like JOptionPane , show it in thread and dispose after 10 sec
制作像 JOptionPane 这样的小框架,在线程中显示并在 10 秒后处理