/**
* 进度条
*
* @time 3:30:01 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Test_ProgressMonitorExample extends JPanel {
private static final long serialVersionUID = 1L;
ProgressThread1 progressThread1;
static JFrame myFrame;
/**
* 构造器
*/
public Test_ProgressMonitorExample() {
setLayout(new BorderLayout());
JPanel Panel = new JPanel();
JButton btnStart = new JButton("Start");
Panel.add(btnStart);
// 开始按钮
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 开始加载
startRunning();
}
});
// 结束按钮
JButton btnStop = new JButton("Stop");
Panel.add(btnStop);
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 停止加载
stopRunning();
}
});
add(Panel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
myFrame = new JFrame("");
Test_ProgressMonitorExample example = new Test_ProgressMonitorExample();
myFrame.getContentPane().add("Center", example);
myFrame.setSize(200, 100);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
myFrame.setVisible(true);
}
/**
* 开始加载
*/
public void startRunning() {
if (null == progressThread1 || progressThread1.isAlive()) {
progressThread1 = new ProgressThread1(this);
progressThread1.start();
}
}
/**
* 停止加载
*/
public void stopRunning() {
progressThread1.setStop(true);
}
}
/**
* 自定义进度线程
*/
class ProgressThread1 extends Thread {
// 操作进度的类
ProgressMonitor monitor;
boolean stopStatus = false;// 状态位
// 最大值 和最小值
int min = 0;
int max = 50;
/**
* 构造器
*/
ProgressThread1(Component parent) {
monitor = new ProgressMonitor(parent, "Progress of Thread", "Not Started", min, max);
}
/**
* 停止进度
*/
public void setStop(boolean value) {
stopStatus = value;
}
@Override
public void run() {
monitor.setNote("Started");
for (int x = min; x <= max; x++) {
if (stopStatus) {
monitor.close();
break;
} else {
monitor.setProgress(x);
monitor.setNote("" + (x * 2) + "%");
try {
sleep(100);
} catch (Exception e) {
}
}
}
}
}
* 进度条
*
* @time 3:30:01 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Test_ProgressMonitorExample extends JPanel {
private static final long serialVersionUID = 1L;
ProgressThread1 progressThread1;
static JFrame myFrame;
/**
* 构造器
*/
public Test_ProgressMonitorExample() {
setLayout(new BorderLayout());
JPanel Panel = new JPanel();
JButton btnStart = new JButton("Start");
Panel.add(btnStart);
// 开始按钮
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 开始加载
startRunning();
}
});
// 结束按钮
JButton btnStop = new JButton("Stop");
Panel.add(btnStop);
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 停止加载
stopRunning();
}
});
add(Panel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
myFrame = new JFrame("");
Test_ProgressMonitorExample example = new Test_ProgressMonitorExample();
myFrame.getContentPane().add("Center", example);
myFrame.setSize(200, 100);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
myFrame.setVisible(true);
}
/**
* 开始加载
*/
public void startRunning() {
if (null == progressThread1 || progressThread1.isAlive()) {
progressThread1 = new ProgressThread1(this);
progressThread1.start();
}
}
/**
* 停止加载
*/
public void stopRunning() {
progressThread1.setStop(true);
}
}
/**
* 自定义进度线程
*/
class ProgressThread1 extends Thread {
// 操作进度的类
ProgressMonitor monitor;
boolean stopStatus = false;// 状态位
// 最大值 和最小值
int min = 0;
int max = 50;
/**
* 构造器
*/
ProgressThread1(Component parent) {
monitor = new ProgressMonitor(parent, "Progress of Thread", "Not Started", min, max);
}
/**
* 停止进度
*/
public void setStop(boolean value) {
stopStatus = value;
}
@Override
public void run() {
monitor.setNote("Started");
for (int x = min; x <= max; x++) {
if (stopStatus) {
monitor.close();
break;
} else {
monitor.setProgress(x);
monitor.setNote("" + (x * 2) + "%");
try {
sleep(100);
} catch (Exception e) {
}
}
}
}
}