用滚动的字符表示Task的运行,比较ProgressMonitorDialog
  sAAkk3Vxfaa8 2023年11月02日 49 0


JFace中的提供的ProgressMonitorDialog对话框,来表示正在运行的Task,还是比较方便,可设置一共的Task有多少步,现在完成了多少,还有多少没有完成的。

来个例子吧:

public class TT {
	static ProgressMonitorDialog dialog;
	public static void main(String[] args) {
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) {
//				monitor.beginTask("begin" + "...... ",10);
				monitor.beginTask("begin" + "...... ",IProgressMonitor.UNKNOWN);
				monitor.setTaskName("Running cmd XXXX.");
				int i = 0;
				while(i++ < 10) {
					if(monitor.isCanceled()) {
						monitor.setTaskName("Canceled cmd XXXX.");
						break;
					}
					try {
						Thread.sleep(1000);
						monitor.setTaskName("Running cmd XXXX.");
						monitor.subTask("Running step " +i + " .");
						monitor.worked(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				monitor.done();
			}
		};
		try {
			dialog = new ProgressMonitorDialog(null);
			dialog.run(true, true, runnable);
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
}

 


 

 

使用IProgressMonitor.UNKNOWN参数,是不知道Task有多少步,Dialog是程序级的对话框,也就模式对话框了。

 

有的时候想搞一些特别的东东来表示程序中有Task正在运行,这里采用滚动的标记字符来表示Task的运行:

例子:

 

public class Tsdf {
	public static  Timer t;
	static Display display = Display.getDefault();
	
	public static void main(String[] args) {
		Shell shell = new Shell();
		shell.setBounds(10,10,150,150);
		shell.setLayout(new RowLayout(SWT.VERTICAL));

		final Label l = new Label(shell, SWT.NONE);
		l.setText("Running");
		final String s = "<<<" + l.getText();
		final int[] index = new int[]{0};
		
		Button brun = new Button(shell, SWT.NONE);
		brun.setText("run");

		Button bstop = new Button(shell, SWT.NONE);
		bstop.setText("stop");

		brun.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(t != null) {
					return;
				}
				t = new Timer(true);
				t.scheduleAtFixedRate(new TimerTask() {
					public void run() {
						System.out.println("running time " + System.currentTimeMillis());
						display.asyncExec(new Runnable() {
							public void run() {
								index[0] = (index[0]<3)?index[0]+1:0;
								if(!l.isDisposed()) {
									l.setText(s.substring(index[0], s.length()-3+index[0]));
								}
							}
						});
					}
				}, 0, 300);
			}
		});
		

		bstop.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if( t == null)
					return;
				t.cancel();
				t = null;
				l.setText("stop");
				System.out.println("Stop time " + System.currentTimeMillis());
			}
		});

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}
}

 

 

结果:


 

运行的效果,label中“<<<Runn”,“<<Runni”,“<Runnin”,“Running”交替出现,给人一种动态的效果。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2oXoBYjmdHYq   2023年12月05日   39   0   0 sedsedMySQLMySQL
  zT6CXotonQAP   2023年12月05日   34   0   0 SystemSystem
sAAkk3Vxfaa8