swt界面下label实时显示时间

      开个线程可解决 

 public  void timeView(Label label) {
	  	
	        
	  	  new Thread() {//线程操作
	            public void run() {
	                while(true){
	                    try {
	                        //对Label进行实时刷新,需要加上这句
	                     label.getDisplay().asyncExec(new Runnable() {       
	                         @Override
	                         public void run() {
	                             // 设置时间 ,格式化输出时间
	                             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"
	                             		+ " HH:mm:ss");
	                             String s = sdf.format(new Date());
	                             label.setText(s);//输出到Label上            
	                         }
	                     });
	                     Thread.sleep(1000);//每隔一秒刷新一次
	                 } catch (Exception e) {
	                 }

	                }
	            }
	     }.start();
	    }

猜你喜欢

转载自blog.csdn.net/qq_40707685/article/details/83745290