进度条问题ProgressMonitor
Java code
import java.awt.Button;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
public class Progress extends JFrame {
private ProgressMonitor pm = new ProgressMonitor(this,"Monitoring Progress","Test",0,100);
Button but = new Button("点击");
public Progress(){
pm.setProgress(0);
pm.setMillisToPopup(1000);
this.add(but);
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
for(int i = 0;i<101;i++){//设置断点,一步一步循环
pm.setProgress(i);
}
}
});
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Progress();
}
}
设置断点后点击按钮,运行到pm.setProgress(i)时,有个框出来,但不是正常的,里面什么都没,而且还卡住了
为什么?请各位指教,谢谢咯
------解决方案--------------------google了个例子。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProgressMonitorDemo extends JPanel
implements ActionListener {
public final static int ONE_SECOND = 1000;
private ProgressMonitor progressMonitor;
private Timer timer;
private JButton startButton;
private LongTask task;
private JTextArea taskOutput;
private String newline = "\n";
public ProgressMonitorDemo() {
super(new BorderLayout());
task = new LongTask();
//Create the demo's UI.
startButton = new JButton("Start");
startButton.setActionCommand("start");
startButton.addActionListener(this);
taskOutput = new JTextArea(5, 20);
taskOutput.setMargin(new Insets(5,5,5,5));
taskOutput.setEditable(false);
add(startButton, BorderLayout.PAGE_START);
add(new JScrollPane(taskOutput), BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
//Create a timer.
timer = new Timer(ONE_SECOND, new TimerListener());
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
progressMonitor.setProgress(task.getCurrent());
String s = task.getMessage();
if (s != null) {
progressMonitor.setNote(s);
taskOutput.append(s + newline);
taskOutput.setCaretPosition(
taskOutput.getDocument().getLength());
}
if (progressMonitor.isCanceled() || task.isDone()) {
progressMonitor.close();
task.stop();
Toolkit.getDefaultToolkit().beep();
timer.stop();
if (task.isDone()) {
taskOutput.append("Task completed." + newline);
} else {
taskOutput.append("Task canceled." + newline);
}
startButton.setEnabled(true);
}
}
}
public void actionPerformed(ActionEvent evt) {
progressMonitor = new ProgressMonitor(ProgressMonitorDemo.this,
"Running a Long Task",
"", 0, task.getLengthOfTask());
progressMonitor.setProgress(0);
progressMonitor.setMillisToDecideToPopup(2 * ONE_SECOND);
startButton.setEnabled(false);