没有按钮的JOptionPane

我需要在屏幕上显示需要5秒的信息消息,在此期间,用户无法关闭对话框。 规范明确指出对话框不应该有任何按钮。 有没有办法可以在对话框没有按钮的情况下使用JoptionPane.showMessageDialog?

如何使用showOptionDialog ,也许不是showMessageDialog ,但是当我们没有按钮或输入文本的地方时(同样可以由用户关闭),同样的事情:

在此处输入图像描述

  JOptionPane.showOptionDialog(null, "Hello","Empty?", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null); 

UPDATE

这是另一种方式,它使用JOptionPaneJDialog (甚至更好,因为它是用户无法关闭的):

在此处输入图像描述

 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); 

看起来David想出了一些东西来满足你对“无按钮”的要求。

话虽如此,听起来你可能需要澄清你真正的要求是什么。 是否真的需要对话框不可关闭,或者没有关闭对话框的按钮? JOptionPane和JDialog有一个关闭按钮,如标准窗口。

我不认为,你可以使用JOptionPane,因为如果我没记错,他们总是至少有一个按钮。 但是你可以使用像这样的启动面板,或者你可以使用普通面板并在其中运行一个线程。 喜欢

 public class TestFrame extends JFrame implements Runnabel{ private Thread thread; private CallerClass c; //Class which built this frame public TestPanel(CallerClass cc){ this.c = cc; this.thread = null; //Window can't be closed on (x) this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); //some things you put into your frame //... this.setVisible(true); } public synchronized void start(){ if (thread == null){ thread = new Thread(this); thread.start(); } @Override public void run() { try{ Thread.sleep(5000); }catch(InterruptedException e){ } this.setVisible(false); this.c.destroyFrame(); this.stop(); } } 

其中destroyFrame()是你的类中的e方法,它构建此面板来销毁它(将其设置为null或其他),你必须使用SwingUtilities.invokeLater(new TestFrame(this))创建这个Frame,如果你不想要其余的图形要冻结。