要在后台线程里对前台界面组件进行访问.
解决方法是使用Display对象,Display对象主要负责管理事件循环和控制UI线程和其它线程之间的通信.
Display.getDefault().asyncExec(new Runnable(){
    public void run(){
    //对前台界面进行操作
    }
});
    - package com.tr069im.ui;  
    
-  
    
- import org.eclipse.swt.SWT;  
    
- import org.eclipse.swt.layout.GridData;  
    
- import org.eclipse.swt.widgets.Button;  
    
- import org.eclipse.swt.widgets.Display;  
    
- import org.eclipse.swt.widgets.MessageBox;  
    
- import org.eclipse.swt.widgets.ProgressBar;  
    
- import org.eclipse.swt.widgets.Shell;  
    
-  
    
-  
    
-  
    
-  
    
-  
    
- public class Login implements Runnable {  
    
-     private static Shell shell;  
    
-     private String loginResponse = "right";  
    
-     private static boolean flag = false;  
    
-  
    
-     public Login() {  
    
-  
    
-     }  
    
-  
    
-     public static void main(String[] args) {  
    
-           
    
-         final Display display = Display.getDefault();  
    
-         shell = new Shell(SWT.MIN);  
    
-         shell.setSize(290, 520);  
    
-         shell.setLocation(300, 5);  
    
-         shell.setText("SWT多线程");  
    
-  
    
-          
    
-         final ProgressBar pb1 = new ProgressBar(shell, SWT.HORIZONTAL  
    
-                 | SWT.SMOOTH);  
    
-         pb1.setBounds(72, 282, 160, 20);  
    
-         pb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));  
    
-          
    
-         pb1.setMinimum(0);  
    
-          
    
-         pb1.setMaximum(30);  
    
-  
    
-          
    
-         final Button btnLogin = new Button(shell, SWT.FLAT | SWT.PUSH);  
    
-         btnLogin.setBounds(80, 363, 111, 36);  
    
-         btnLogin.setText("取消");  
    
-  
    
-         shell.open();  
    
-          
    
-         display.asyncExec(new Runnable() {  
    
-             public void run() {  
    
-                 Login pl = new Login();  
    
-                 Thread t = new Thread(pl);  
    
-                 t.start();  
    
-             }  
    
-         });  
    
-          
    
-         Runnable runnable = new Runnable() {  
    
-             public void run() {  
    
-                 for (int i = 0; i < 30; i++) {  
    
-                     try {  
    
-                         Thread.sleep(100);  
    
-                     } catch (InterruptedException e) {  
    
-                     }  
    
-                     display.asyncExec(new Runnable() {  
    
-                         public void run() {  
    
-                             if (pb1.isDisposed())  
    
-                                 return;  
    
-                              
    
-                             pb1.setSelection(pb1.getSelection() + 1);  
    
-                         }  
    
-                     });  
    
-                     if (flag) {  
    
-                         break;  
    
-                     }  
    
-                     if (i == 29) {  
    
-                         open();  
    
-                     }  
    
-                 }  
    
-             }  
    
-         }; 
    
-         new Thread(runnable).start();  
    
-  
    
-         while (!shell.isDisposed()) {  
    
-             if (!display.readAndDispatch()) {  
    
-                 display.sleep();  
    
-             }  
    
-         }  
    
-         display.dispose();  
    
-     }  
    
-  
    
-     public void run() {  
    
-         try {  
    
-              
    
-             if (loginResponse.equals("right")) {  
    
-                  
    
-                 Display.getDefault().asyncExec(new Runnable() {  
    
-                     public void run() {  
    
-                         flag = true;  
    
-                         Shell shell = new Shell(SWT.MIN);  
    
-                         MessageBox messageBox = new MessageBox(shell,  
    
-                                 SWT.ICON_WARNING);  
    
-                         messageBox.setMessage("用户名或密码错误!");  
    
-                         messageBox.open();  
    
-                     }  
    
-                 });  
    
-  
    
-             } else {      
    
-             }  
    
-         } catch (Exception ee) {  
    
-         }  
    
-     }  
    
-  
    
-     public static void open() {  
    
-         Display.getDefault().asyncExec(new Runnable() {  
    
-             public void run() {  
    
-                  
    
-             }  
    
-         });  
    
-     }  
    
- }  
	posted on 2011-04-17 21:57 
Soap MacTavish 阅读(1466) 
评论(0)  编辑  收藏