好的人,我有这个示例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()&gt; = 2000的那个,但对我来说都不起作用。
你能帮我找一个解决方案吗?先感谢您。
答案 0 :(得分:0)
我不确定你的问题......但这确实有效:
public void iterate() {
while (num < 2000) {
current.setValue(num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
num += 95;
if (num >= 2000)
System.exit(0);
}
}
答案 1 :(得分:0)
您可以在构建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;
}