【问题标题】:How to exit an app when ProgressBar reaches 100?当 ProgressBar 达到 100 时如何退出应用程序?
【发布时间】:2012-12-27 17:04:38
【问题描述】:

好的,我有这个示例 Java 代码。你们中的许多人可能以前见过它。由于我对 Java 很陌生,我想知道在 ProgressBar 达到 100% 或在我的情况下是 num >= 2000 后,您实际上是如何调用程序来关闭的?

代码:

    package progress;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ProgressMonitor extends JFrame {
JProgressBar current;
JTextArea out;
JButton find;
Thread runner;
int num = 0;

public ProgressMonitor()
{
    super("Progress monitor");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(205,68);
    setLayout(new FlowLayout());
    current = new JProgressBar(0,2000);
    current.setValue(0);
    current.setStringPainted(true);
    add(current);
}
public void iterate()
{
    while(num<2000){
    current.setValue(num);
    try{
        Thread.sleep(1000);

    }catch (InterruptedException e) { }
    num+=95;
    }
}   


    public static void main(String[] args) {
        ProgressMonitor pm = new ProgressMonitor();
        pm.setVisible(true);
        pm.iterate();

    }

}

我尝试在 while 块中使用 if 语句,所以我写了

if(num >=2000) System.exit(0);

但什么也没发生。

我还尝试转换 JProgressBar getValue() 方法并将其装箱为整数

if ((Integer)current.getValue() >= 100) System.exit(0);

和 current.getValue() >= 2000 的那个,但对我都不起作用。

你能帮我找到解决办法吗?先感谢您。

【问题讨论】:

    标签: java swing progress-bar jframe jprogressbar


    【解决方案1】:

    您可以在构建 JFrame 时检查 javadoc:

    public interface WindowConstants
    {
        /**
         * The do-nothing default window close operation.
         */
        public static final int DO_NOTHING_ON_CLOSE = 0;
    
        /**
         * The hide-window default window close operation
         */
        public static final int HIDE_ON_CLOSE = 1;
    
        /**
         * The dispose-window default window close operation.
         * <p>
         * <b>Note</b>: When the last displayable window
         * within the Java virtual machine (VM) is disposed of, the VM may
         * terminate.  See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
         * AWT Threading Issues</a> for more information.
         * @see java.awt.Window#dispose()
         * @see JInternalFrame#dispose()
         */
        public static final int DISPOSE_ON_CLOSE = 2;
    
        /**
         * The exit application default window close operation. Attempting
         * to set this on Windows that support this, such as
         * <code>JFrame</code>, may throw a <code>SecurityException</code> based
         * on the <code>SecurityManager</code>.
         * It is recommended you only use this in an application.
         *
         * @since 1.4
         * @see JFrame#setDefaultCloseOperation
         */
        public static final int EXIT_ON_CLOSE = 3;
    
    }
    

    【讨论】:

      【解决方案2】:

      我不太确定你的问题......但这确实有效:

      public void iterate() {
          while (num < 2000) {            
              current.setValue(num);
              try {
                  Thread.sleep(500);
      
              } catch (InterruptedException e) {
              }
      
              num += 95;
      
              if (num >= 2000)
                  System.exit(0);
          }
      }
      

      【讨论】:

      • 嗯...很奇怪,但谢谢。就我而言,它根本不起作用,但我找到了一个类似的解决方案if(current.getValue() &gt;= (2000-95)){ System.exit(0);,它确实有效。
      • 也许你把if语句放在num += 95之前?如果没有,我无法弄清楚为什么代码不起作用
      • Thread.sleep(500); 不要阻塞 EDT(事件调度线程)——当这种情况发生时,GUI 将“冻结”。而不是调用Thread.sleep(n) 实现一个Swing Timer 用于重复任务或SwingWorker 用于长时间运行的任务。有关详细信息,请参阅Concurrency in Swing
      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多