Shell 窗体进度条

来源:互联网 发布:mac硬盘取消密码 编辑:程序博客网 时间:2024/05/16 09:39

 import java.lang.reflect.InvocationTargetException;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.jface.dialogs.ProgressMonitorDialog;import org.eclipse.jface.operation.IRunnableWithProgress;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;// 循环多次public class TestProgress {    static boolean stopflg = false;    /**     * Launch the application     * @param args     */    public static void main(String[] args) throws Exception{        final Display display = Display.getDefault();        final Shell shell = new Shell();        shell.setSize(500, 375);        shell.setText("SWT Application");                //        IRunnableWithProgress runnable = new IRunnableWithProgress(){            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {                monitor.beginTask("generate", 30);                int i=0;                while(true){                    if(stopflg){                        break;                    }                    i++;                    if(i==30){                        i=0;                        monitor.beginTask("generate", 30);                    }                    monitor.worked(1);                    Thread.sleep(100);                }                monitor.done();            }        };        new ProgressMonitorDialog(shell).run(true, true, runnable);        shell.open();        shell.layout();        while (!shell.isDisposed()) {            if (!display.readAndDispatch())                display.sleep();        }    }}
import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.jface.dialogs.ProgressMonitorDialog;import org.eclipse.jface.operation.IRunnableWithProgress;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class TestProgress {    static boolean stopflg = false;    /**     *循环一次     * Launch the application     * @param args     */    public static void main(String[] args) throws Exception{        final Display display = Display.getDefault();        final Shell shell = new Shell();        shell.setSize(500, 375);        shell.setText("SWT Application");                //        IRunnableWithProgress runnable = new IRunnableWithProgress(){            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {                monitor.beginTask("generate", 30);                for(int i=0;i<100;i++){                    if(monitor.isCanceled()){                        return;                    }                    monitor.worked(1);                    Thread.sleep(50);                }                                monitor.done();            }        };        new ProgressMonitorDialog(shell).run(true, true, runnable);        shell.open();        shell.layout();        while (!shell.isDisposed()) {            if (!display.readAndDispatch())                display.sleep();        }    }}


0 0