SWTUtil.java
package xiangjie.demo;
import org.eclipse.swt.widgets.Shell;
* @author 向潔
* AWTUtil工具封裝類
*/
public class SWTUtil {
/*創建靜態顯示對象*/
private static Display display =new Display();
/*創建靜態窗口對象方法*/
public static Shell getShell()
{
Shell shell=new Shell(display);
return shell;
}
/*打開窗口靜態方法*/
public static void openShell(Shell shell)
{
shell.open();
/*此處循環用於解決窗口一閃而過問題*/
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();//等待事件
}
HelloWorld.java
package xiangjie.test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import xiangjie.demo.SWTUtil;
/**
* @author 向潔
* AWT 使用
*/
public class HelloWorld {
public static void main(String[] args)
{
/*獲取到靜態窗口對象*/
Shell shell=SWTUtil.getShell();
shell.setText("酒店管理系統");//
/*設置窗口主題*/
shell.setLayout(new GridLayout(2,true));
/*創建組件添加到窗口shell*/
new Label(shell,SWT.NONE).setText("Label");
new Button(shell,SWT.CHECK).setText("Button");
new Text(shell,SWT.BACKGROUND).setText("Text");
shell.setSize(300,200);
SWTUtil.openShell(shell);
}
}