【问题标题】:Close dialog box automatically自动关闭对话框
【发布时间】:2013-08-28 16:24:03
【问题描述】:

我有一个关于JOptionPane 的问题,关于如何自动关闭对话框,我可以使用该对话框在事件中提醒用户并在延迟后自动关闭而无需关闭对话框?

public class ShutdownComputer
{  
    public static void main(String[]args)  
    {  
        int wholeTimeBeforeShutdown=0;  

        int hour=0;  
        int minute=0;  
        int second=0;  

        try  
        {  
            String a=JOptionPane.showInputDialog(null,"HOUR","HOUR BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  
            String b=JOptionPane.showInputDialog(null,"MINUTE","MINUTE BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  
            String c=JOptionPane.showInputDialog(null,"SECOND","SECOND BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  

            if((a==null)||(a.equals("")))  
            {  
                a="0";  
            }  

            if((b==null)||(b.equals("")))  
            {  
                b="0";  
            }  

            if((c==null)||(c.equals("")))  
            {  
                c="0";  
            }  

            int e=Integer.parseInt(a);  
            int f=Integer.parseInt(b);  
            int g=Integer.parseInt(c);  

            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+((e*60)*60);  
            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(f*60);  
            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(g);  

            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown*1000;  


            Thread.sleep(wholeTimeBeforeShutdown);  

            JOptionPane.showMessageDialog(null, "You only have less than 2 minutes left"); 

            Runtime.getRuntime().exec("shutdown -r -f -t 120");  
        }  
        catch(Exception exception)  
        {  
            exception.printStackTrace();  
        }  
    }  
}

【问题讨论】:

    标签: java swing timer modal-dialog joptionpane


    【解决方案1】:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.concurrent.TimeUnit;
    
    import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    

        final String msg="Shutdown\nWill happen automatically in "; 
        final JOptionPane op=new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);
        final JDialog d=op.createDialog("Shutdown");
        d.setModal(true);
        // deadline in three hours:
        final long deadline=System.nanoTime()+TimeUnit.HOURS.toNanos(3);
        ActionListener updater=new ActionListener() {
          public void actionPerformed(ActionEvent e)
          {
            long left=deadline-System.nanoTime();
            if(left<=0) {
              d.dispose();
              return;
            }
            String time;
            long t=TimeUnit.NANOSECONDS.toHours(left);
            if(t>0) time=t>1? t+" hours": "one hour";
            else {
              t=TimeUnit.NANOSECONDS.toMinutes(left);
              time=t>1? t+" minutes": "one minute";
            }
            op.setMessage(msg+time);
          }
        };
        updater.actionPerformed(null);//update initial message
        d.pack(); // resize dialog for the updated message
        final Timer t=new Timer(1000, updater);
        t.setInitialDelay(0);
        t.setRepeats(true);
        t.start();
        d.setVisible(true);
        t.stop();
        // do your action
    

    请注意,此代码发出的消息将舍入时间值,但我认为这对您来说是一个可用的起点。

    【讨论】:

    • 效果很好!如何将 JDialog x 按钮设置为禁用或如何删除 x 按钮?我已经尝试了代码,但是当我单击 x 按钮时,它停止运行。请帮忙。非常感谢。
    • 在我的测试中,按下窗口关闭符号就像按下确定一样。但是调用d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 应该完全禁止✖。如果提供的默认操作选项都不适合,您可以添加一个 WindowListener 来决定在 windowClosing 上做什么。
    • 另外,您可以在创建对话框后立即通过d.dispose(); d.setUndecorated(true); d.getRootPane().setBorder(BorderFactory.createEtchedBorder()); 删除所有窗口控件。
    【解决方案2】:

    您可能需要使用 JOptionPane.setVisible(false)。我没试过,但是http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html底部的例子可以给你一个提示。

    【讨论】:

    猜你喜欢
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多