一 、新建工程 ,名为myswt
二 、建立窗体
Next -- 命名为HelloWorld ,包名为com.swtdesigner .
三 、HelloWorld.java中的代码如下 :
package com.swtdesigner ;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
publicclass HelloWorld{
publicstaticvoid main(String args []){
final Display display = Display.getDefault();
final Shell shell = new Shell() ;
shell.setSize(357,259) ;
shell.setText("SWT Application") ;
//插入Text对象
Text text = new Text(shell,SWT.BORDER) ;
//addMouseListener加入鼠标事件的监听器
text.addMouseListener(new MouseAdapter(){
publicvoid mouseDoubleClick(MouseEvent e){
MessageDialog.openInformation(null,"","Double Click !") ;
}
});
text.setText("Hello World !") ;
text.setBounds(50, 50, 100, 24) ;
shell.layout() ;
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep() ;
}
}
}导入的类中 前三个是匿名内部事件所用到的
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
运行结果 :
双击“HelloWorld!”会弹出一个对话框 ,如图所示 :