【问题标题】:JOptionPane without button没有按钮的 JOptionPane
【发布时间】:2012-12-17 02:39:40
【问题描述】:

我需要显示一条信息消息,需要在屏幕上显示 5 秒钟,在此期间,用户无法关闭对话框。规范清楚地表明对话框不应该有任何按钮。 有没有办法以对话框没有按钮的方式使用 JoptionPane.showMessageDialog?

【问题讨论】:

  • 也许为您的消息创建一个带有 JLabel 的 JWindow(因此它没有装饰,并且在标题栏中没有“关闭”按钮等)会满足您的需求吗?然后只需创建一个 Timer 即可自动关闭它。

标签: java swing joptionpane


【解决方案1】:

我不认为您可以使用 JOptionPane,因为如果我没记错的话,它们至少总是有一个按钮。 但是您可以使用例如this 之类的启动面板,或者您可以使用普通面板并在其中运行一个线程。 喜欢

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冻结。

【讨论】:

  • -1 这段代码看起来根本无法编译和/或运行,也违反了很多 Swing 最佳实践
  • 这不是整个代码,它应该只显示重要的东西,我认为它会起作用..
  • 我同意大卫的观点,使用线程来控制对话,与 EDT 重新同步是不好的做法,特别是当 javax.sing.Timer 就足够了时
  • @alexvii : 这不是完整的代码,它应该只显示重要的东西,我认为它会起作用 在阅读了这行 @987654324 之后,再次问自己这个问题@,类扩展JPanel。你还认为它可以运行吗?
  • @GagandeepBali :谢谢,这是对的,我不知道为什么,但我把 Frame 和 Panel 搞混了,它真的应该是 JFrame .. 在那里你可以使用“setDefaultCloseOperation”
【解决方案2】:

这种方式使用showOptionDialog怎么样,可能不是showMessageDialog,但是当我们没有按钮或输入文本的地方时也是一样的(缺点是它可以被用户关闭):

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

更新

这是另一种方式,它使用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);

【讨论】:

  • @joeyrohan 是的,在任何添加组件的窗口上总是需要pack()
  • 哎呀,是的,有;)问是因为我认为我们不在JOptionPane.showMessageDialog
  • @joeyrohan 没问题...JOptionPane 是一个包含显示简单消息的方法的类,它们可能从类本身内部调用pack()
  • 谢谢,这正是我想要的:)
【解决方案3】:

看起来大卫想出了一些东西来满足您“无按钮”的要求。

话虽如此,听起来您可能需要澄清您的真正要求。真的需要对话框不可关闭,还是没有按钮可以关闭对话框? JOptionPane 和 JDialog 有一个类似于标准窗口的关闭按钮。

【讨论】:

    【解决方案4】:

    我使用上面的一些代码来创建一个“方法”来外部调用。因此,这可以允许多个弹出窗口。我的版本允许更改消息类型、标题、显示时间、屏幕位置、文本,并提供更改文本消息字体和颜色的示例。

    /*
     * LabelDemo.java contains a method that allow multiple popups. This version allows
     * changing the message type, title, display time, screen placement, text and
     * provides examples of changing the font and color of the text message.
     */ 
    
    package components;
    
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    
    /*
     * LabelDemo.java 
     * 
     */
    public class LabelDemo {
       public LabelDemo() {
       }
    
       public static void main(String[] args) {
          TextDisplayPopup("1st popup", "<html><font size=11 color=blue>information type",
                6, 50, 105, JOptionPane.INFORMATION_MESSAGE);
          TextDisplayPopup("2nd popup", "<html><font size=15 color=red>ERROR TYPE\n"
                + "<html><font size=6 color=green>2nd line with long sentence for checking"
                + " popup box dynamic sizing.",
                10, 240, 240, JOptionPane.ERROR_MESSAGE);
       }
    
       public static void TextDisplayPopup(String strTitle, String strText,
             int iDelayInSeconds, int iX_Location, int iY_Location, int iMessageType) {
          final JOptionPane optionPane = new JOptionPane(strText,
                iMessageType, JOptionPane.DEFAULT_OPTION,
                null, new Object[]{}, null);
          final JDialog dialog = new JDialog();
          dialog.setTitle(strTitle);
          dialog.setModal(false);
          dialog.setContentPane(optionPane);
          dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
          dialog.pack();
          dialog.setLocation(iX_Location, iY_Location);
    
          //create timer to dispose of dialog after, iDelayInSeconds, seconds
          Timer timer = new Timer(iDelayInSeconds*1000, new AbstractAction() {
              @Override
              public void actionPerformed(ActionEvent ae) {
                  dialog.dispose();
              }
          });
          timer.setRepeats(false);  //  one-time use timer
    
          //  timer removes dialog after iDelayInSeconds
          timer.start();
          dialog.setVisible(true);
          System.out.println("end of test");
       }
    }
    

    输出: 1st popup

    2nd popup

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-27
      • 2012-01-29
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2013-12-05
      相关资源
      最近更新 更多