SWT第一个程序测试

  • Post author:
  • Post category:其他


首先需要下载SWT插件,下载后建立SWT工程。

import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;
public class HelloSwt extends Shell {



private static Text text;


private static Button swtButton;


private static Button swingButton;


private static Button awtButton;


private static Group group;


private static Button button;


private static Label benefitOfSwtLabel;


private static List list;


public static void main(String[] args) {


// TODO Auto-generated method stub


Display display=Display.getDefault();


final Shell shell=new Shell(display);


shell.setText(“Hello Swt”);


shell.setSize(260, 283);


shell.open();



//创建一个文本框


text=new Text(shell,SWT.BORDER);


text.setText(“swt是图形工具箱”);


text.setBounds(10,8,230,35);



//创建标签标注


benefitOfSwtLabel=new Label(shell,SWT.None);


benefitOfSwtLabel.setText(“swt 的优点”);


benefitOfSwtLabel.setBounds(10,49,90,15);



//创建一个列表


list=new List(shell,SWT.BORDER);


list.setItems(new String[]


{


“使用操作系统本地控件”,


“提供一套平台无关的API”,


“gui程序的运行速度快”,


“更多。。。”


}


);


list.setBounds(10,68,232,82);



//group


group=new Group(shell,SWT.None);


group.setText(“你使用过那些图形工具箱?”);


group.setBounds(10,159,230,47);



//在group里放置多选框


awtButton=new Button(group,SWT.CHECK);


awtButton.setText(“AWT”);


awtButton.setBounds(10,20,54,18);



swingButton=new Button(group, SWT.CHECK);


swingButton.setText(“swing”);


swingButton.setBounds(70, 22, 60, 15);



swtButton=new Button(group, SWT.CHECK);


swtButton.setText(“swt”);


swtButton.setBounds(136, 22, 62, 15);





//事件监听按钮


button=new Button(shell, SWT.None);


button.addSelectionListener(new SelectionAdapter() {


public void widgetSelected(final SelectionEvent e)


{


MessageBox msgbox=new MessageBox(shell, SWT.ICON_INFORMATION);


msgbox.setMessage(“Hello SWT!”);


msgbox.open();


}


});


button.setText(“按下按钮,说hello”);


button.setBounds(10,214,227,25);






//shell.layout();


//界面停住


while(!shell.isDisposed())


{


if(!display.readAndDispatch())


display.sleep();


}




}
}



版权声明:本文为sinat_27612639原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。