没有按钮的JOptionPane [英] JOptionPane without button
问题描述
我需要在屏幕上显示需要5秒钟的信息消息,在此期间,用户无法关闭对话框。规范明确指出对话框不应该有任何按钮。
我有没有办法以对话框没有按钮的方式使用JoptionPane.showMessageDialog?
I need to present a information message that needs to be in the screen for 5 seconds, during this time, user can't close the dialog. The specification says clearly that the dialog shouldn't have any button. Is there a way I can use JoptionPane.showMessageDialog in a way that the dialog have no button?
推荐答案
关于这种方式使用 showOptionDialog
,可能不是 showMessageDialog
,但是当我们没有按钮或输入文本的地方时也是如此(可以由用户关闭):
How about this way using showOptionDialog
, maybe not showMessageDialog
, but the same thing when we have no buttons or place to enter text (downfall is it can be closed by user):
JOptionPane.showOptionDialog(null, "Hello","Empty?", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
更新
这是另一种方式,它使用 JOptionPane
和 JDialog
(甚至更好,因为用户无法关闭):
Here is another way, it uses JOptionPane
and JDialog
(even better as it is uncloseable by user):
final JOptionPane optionPane = new JOptionPane("Hello world", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
final JDialog dialog = new JDialog();
dialog.setTitle("Message");
dialog.setModal(true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
//create timer to dispose of dialog after 5 seconds
Timer timer = new Timer(5000, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
dialog.dispose();
}
});
timer.setRepeats(false);//the timer should only go off once
//start timer to close JDialog as dialog modal we must start the timer before its visible
timer.start();
dialog.setVisible(true);
这篇关于没有按钮的JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!