swing学习笔记五(进度条ProgressMonitor )

/**
 * 进度条
 * 
 * @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) {
}
}
}
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值