我有一个Jbutton“Highlight”,当点击时调用Jprogress Bar。进度条可以正常工作到100%。
如果进度条达到100%后,我怎样才能显示结果。
以下是代码的一部分:
final JProgressBar progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread t = new Thread(new Runnable() {
public void run() {
int i = 1;
progressBar.setMinimum(0);
progressBar.setMaximum(100);
try {
while (i <= 100 || true) {
progressBar.setValue(i);
i++;
Thread.sleep(38);
}
} catch (InterruptedException ex){}
}});
t.start();
//我应该把System.out.println放在哪里(“Jprogress Bar达到100%”??
答案 0 :(得分:1)
在i ++之后添加if语句:)
i++;
if (i == 100) {
displayResult();
return;
}
不要忘记使用invokeAndWait。当你从错误的线程操作GUI控件时,当前代码很可能无法正常运行。
答案 1 :(得分:0)
try{
while (i <= 100 || true) {
progressBar.setValue(i);
i++;
Thread.sleep(38);
}
// progress is definitely at 100 now
// do whatever you need to display result here -- it would be faster than doing the check
// if(i==100) in every execution inside the while loop.
System.out.println("Progress is done!!!");
} catch (....