package com.yc.ui;
import java.util.Date;
public class Da {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Da window = new Da();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button button = new Button(shell, SWT.NONE);
button.addDisposeListener(new DisposeListener() {
//关闭窗口
public void widgetDisposed(DisposeEvent arg0) {
System.exit(0);
}
});
button.setBounds(106, 104, 80, 27);
button.setText("New Button");
final Label label = new Label(shell, SWT.NONE);
label.setBounds(106, 169, 236, 65);
label.setText("New Label");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Show s=new Show(label);
Thread t=new Thread(s);
t.start();
}
});
}
}
class Show implements Runnable{
private Label label;
public Show(Label label){
this.label=label;
}
@Override
public void run() {
Display display=label.getDisplay().getDefault();
while(true){
//调用label的syncExec方法
display.syncExec(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
label.setText(new Date()+"");
}
});
}
}
}
解决SWT多线程的问题
最新推荐文章于 2024-07-11 00:00:00 发布