我的想法是按“启动”时,开始循环,按“停止”时,停止循环
现在的问题是,启动开始循环没有问题,但是按下停止时,进度条依然在前进,没有停止
请指点
按钮的代码
MyThread r2 = new MyThread(sd1);
if ( btnSd.getText().equals("启动")) {
r2.bStop = true;
r2.start();
btnSd.setText("停止");
}
else {
r2.bStop = false;
r2.interrupt();
btnSd.setText("启动");
}
线程是继承Thread
public class MyThread extends Thread {
private JSlider iSd; //进度条控件,JSlider
public boolean bStop; //是否要终止线程
//类的构造函数
public MyThread(JSlider sd) {
iSd = sd;
nType = 2;
}
//重写run方法
@Override
public void run() {
for(int i=0;i<10;i++) {
if( !bStop) {
break; //用户终止线程
}
iSd.setValue(0);
int nPos;
while(iSd.getValue()<iSd.getMaximum()){
System.out.println(bStop);
if ( !bStop) {
break; //用户终止线程
}
nPos = iSd.getValue();
nPos++;
iSd.setValue(nPos);
try {
this.sleep(10);
}
catch(InterruptedException e) {
System.out.println("err");
}
}
}
}
}