通过如下方式:
/************************ 实现定时器 ***********************************/
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 /****************************************************************/
完整示例如下
- 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();
- }
- }