📄 progressthread.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
public class ProgressThread extends Thread implements PropertyChangeListener {
/**
* @param args
*/
private JProgressBar jpb;
private Task task;
class Task extends SwingWorker<Void, Void> {
/*
* Main task. Executed in background thread.
*/
ZipDirectory zd;
Random r=new Random();
public Task(String file) throws Exception {
zd=new ZipDirectory(file);
}
public Void doInBackground() {
try {
zd.zip();
} catch (Exception e) {
}
return null;
}
protected void procress(){
// this.setProgress(r.nextInt(30));
}
public void done(){
System.out.print("ZDone");
}
}
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
jpb.setValue(progress);
}
}
public ProgressThread(JProgressBar pb) throws Exception{
jpb=pb;
task=new Task("C:\\Download");
}
public void run(){
int min=jpb.getMinimum();
int max=jpb.getMaximum();
Runnable runner=new Runnable(){
public void run(){
int value = jpb.getValue();
jpb.setValue(value+1);
}
};
for(int i=min;i<max;i++){
try {
SwingUtilities.invokeAndWait(runner);
Thread.sleep(500);
} catch (InterruptedException e) {
}catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
final JProgressBar aJProgressBar = new JProgressBar(0, 100);
final JButton aJButton = new JButton("Start");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// aJButton.setEnabled(false);
// aJProgressBar.setIndeterminate(true);
Thread stepper;
try {
stepper = new ProgressThread(aJProgressBar);
stepper.start();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
aJButton.addActionListener(actionListener);
JFrame theFrame = new JFrame("Progress Bars");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = theFrame.getContentPane();
contentPane.add(aJProgressBar, BorderLayout.NORTH);
contentPane.add(aJButton, BorderLayout.SOUTH);
theFrame.setSize(300, 100);
theFrame.setVisible(true);
// task.addPropertyChangeListener(this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -