此处有线程的相关操作——(尚未学习)
进度条通常要与线程同时使用,即后台运行一个线程,同时将后台程序运行的状态以进度条的方式显示出来。
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class G{
public static void main(String[] args){
final Display display=new Display();
Shell shell=new Shell(display);
shell.setText("ProgressBar进度条");
shell.setLayout(new GridLayout());
//创建一个进度条
final ProgressBar progressBar=new ProgressBar(shell,SWT.HORIZONTAL);
//设置最大值最小值
progressBar.setMaximum(100);
progressBar.setMinimum(0);
//获得最大值最小值
final int maximum=progressBar.getMaximum();
final int minimum=progressBar.getMinimum();
//创建一个线程
Runnable runnable=new Runnable(){
public void run(){
for(int i=minimum;i<maximum;i++){//更新进度条的次数
//尝试让让这个人先睡0.1秒,目的是让进度条的进度变慢,看得更加清楚一些
try{
Thread.sleep(100);
}
catch(InterruptedException e){
}
//UI线程更新进度条的值
display.asyncExec(new Runnable(){
public void run(){
if(progressBar.isDisposed()){
return;
}
else{
progressBar.setSelection(progressBar.getSelection()+1);
}
}
});
}
}
};
//启动线程
new Thread(runnable).start();
//打开窗口,进行窗口的显示
shell.setSize(250,100);
//shell.pack();
shell.open();
while(!shell.isDisposed()){
//当窗口没有被释放的时候
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}