經過以下方式:shell
/************************ 實現定時器 ***********************************/
final int time=1000;
Runnable showTime = new Runnable(){
public void run(){
System.out.println("swt定時器的實現!");
display.timerExec(time, this);
}
};
display.timerExec(time,showTime);//你的swt程序的display
/****************************************************************/eclipse
完整示例以下ide
- package test.ftp00;
- import org.eclipse.swt.graphics.Rectangle;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Monitor;
- import org.eclipse.swt.widgets.Shell;
- public class TimerWindow {
- Display display;
- Shell shell;
- GridLayout gridLayout;
- GridData layoutData;
- Composite composite;
- static String xmlDir[];
- public TimerWindow() {
- display = Display.getDefault();
- // shell = new Shell(display, SWT.NO_TRIM );
- shell = new Shell(display);
- // 初始化shell
- initShell();
- /************************ 實現定時器 ***********************************/
- final int time = 1000;
- Runnable showTime = new Runnable() {
- public void run() {
- System.out.println("swt定時器的實現!");
- display.timerExec(time, this);
- }
- };
- display.timerExec(time, showTime);// 你的swt程序的display
- /****************************************************************/
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- }
- /**
- * 設置窗口的標題、位置、大小、圖標
- *
- * @return Shell
- */
- public Shell initShell() {
- shell.setText("添加內容");
- shell.setSize(160, 500);
- Monitor monitor = shell.getMonitor();
- Rectangle bounds = monitor.getBounds();
- Rectangle rect = shell.getBounds();
- shell.setLocation(bounds.width - 200 - rect.width, bounds.y);
- shell.setLayout(new FillLayout());
- return shell;
- }
- public static void main(String[] args) {
- new TimerWindow();
- }
- }