没有按钮的JOptionPane

问题描述 投票:13回答:4

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

java swing joptionpane
4个回答
35
投票

如何使用showOptionDialog,也许不是showMessageDialog,但是当我们没有按钮或位置来输入文本时,情况也是一样(缺点是可以由用户关闭):

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9GVzd5ai5wbmcifQ==” alt =“在此处输入图像描述”>

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

1
投票

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

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


0
投票

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

/*
 * 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


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

Where destroyFrame()是类中的e方法,用于构建该面板以销毁它(将其设置为null或其他东西),并且如果您不希望其余图形被创建,则必须使用SwingUtilities.invokeLater(new TestFrame(this))创建此Frame。冻结。

© www.soinside.com 2019 - 2024. All rights reserved.