首页 > 其他分享 >用滚动的字符表示Task的运行,比较ProgressMonitorDialog

用滚动的字符表示Task的运行,比较ProgressMonitorDialog

时间:2023-10-13 19:05:40浏览次数:29  
标签:ProgressMonitorDialog 字符 Task monitor 10 static new public


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”交替出现,给人一种动态的效果。

标签:ProgressMonitorDialog,字符,Task,monitor,10,static,new,public
From: https://blog.51cto.com/u_16298170/7850437

相关文章

  • hashcat charsets文件夹下 .hcchr自定义字符集
    说明通常我们使用的是英文字符,abc123这些,但是如果遇到俄文、阿拉伯等一些非ASCII码的字符集,每次手动输入不现实。所以就可以保存到.hcchr文件中一、先看看,hashcat提供內建字符集?l=abcdefghijklmnopqrstuvwxyz?u=ABCDEFGHIJKLMNOPQRSTUVWXYZ?d=0123456789?s=......
  • Python JSON 库对 UTF8 字符的处理方式分析
    默认情况在使用json模块的json.dump时,默认会将非ASCII字符(中文字符等)进行Unicode转义,保证最终文件只有ASCII字符。例如下述代码:importjsonwithopen("text.json","w")asf:data={'1':111,'2':"你好",'3':"Hello",�......
  • 09_删除字符串中的所有相邻重复项
    删除字符串中的所有相邻重复项【题目】给出由小写字母组成的字符串S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在S上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。本题对应于leetcode1047示例:输入:"......
  • 14.3 Socket 字符串分块传输
    首先为什么要实行分块传输字符串,一般而言Socket套接字最长发送的字节数为8192字节,如果发送的字节超出了此范围则后续部分会被自动截断,此时将字符串进行分块传输将显得格外重要,分块传输的关键在于封装实现一个字符串切割函数,将特定缓冲区内的字串动态切割成一个个小的子块,当切割结......
  • java的字符串模板
    java的字符串模板介绍java如何解决字符串占位符的问题JEP430字符串在java中是如何构造的在编程中,字符串是无处不在的。在编码过程中,要不断的构造字符串直接使用连字符+对于很短的字符连接是很方便的,但对于多个+操作,就十分麻烦了并且很难读StringBufferStringBuilder可......
  • Prefixes and Suffixes (CF D) (字符串翻转找性质)
     思路:利用操作使得题目更好分析,t的后缀,反转t,来看t的前缀, 实际操作的时候,把s和t的前缀在反转一下进行交换就可以了,发现性质1C(si,ti)他们的相对位置不会变化,一直是匹配的然后利用翻转的性质,一定会产生任意我想要的排列 (从后开始构造,先把目......
  • JNI编程之字符串处理
    java中的字符串类型是String,对应的jni类型是jstring,由于jstring是引用类型,所以我们不能像基本数据类型那样去使用它,我们需要使用JNIEnv中的函数去处理jstring,下面介绍一些常用的字符串处理函数。1.GetStringUTFChars()作用:将jstring类型转化为c中的constchar*类......
  • 数据库解决获取一个字段parent中某个字符串child第一次和第二次出现的位置之间的内容c
    下面就postgresql数据和oracle数据库分别提供两种解决方法--postgresql数据库解决获取一个字段parent中某个字符串child第一次和第二次出现的位置之间的内容cut--方法一selectcasewhenposition(childinparent)>0thensubstring(parent,position(childinparent)+l......
  • 解决 springboot 2.6.6 版本中内嵌 tomcat 9.0.60 版本 严格执行RFC 3986规范,导致在 G
    项目中get请求:http://domain:port/api/module/function/getList?pageNum=1&pageSize=1000&keyWord=[]&id=;keyWord的参数应该是 keyWord="[]",如果不加双引号,keyWord就变成了数组,后台接口就报错了。调查原因:springboot2.6.6版本中内嵌tomcat9.0.60版本严格执行RFC3986规范......
  • 使用python来对字符编码序列进行互转
    排查字符集问题时,有的时候发生乱码不知道如何生成的字符,此时就需要通过字节序列来判断该字符是什么。已知utf8字节序列时,转换为unicode或者gb18030字节序列:>>>a=b'\xef\xbc\xa1'#此时a是一个bytes对象>>>b=a.decode("utf8")#此时b是一个str对象,内部是unicode的编码字......