有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

不带按钮的java JOptionPane

我需要提供一条信息消息,需要在屏幕上显示5秒钟,在此期间,用户无法关闭对话框。规范明确规定对话框不应该有任何按钮。 有没有一种方法可以使用JoptionPane。是否以对话框没有按钮的方式显示MessageDialog


共 (4) 个答案

  1. # 1 楼答案

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

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

  2. # 2 楼答案

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

    enter image description here

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

    更新

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

    enter image description here

    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);
    
  3. # 3 楼答案

    我不认为你可以使用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()是类中的一个方法,用于构建此面板以销毁它(将其设置为null或其他值),如果不希望其他图形冻结,则必须使用SwingUtilities.invokeLater(new TestFrame(this))创建此框架

  4. # 4 楼答案

    看起来大卫想出了一些办法来满足你的“无按钮”要求

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