实验三:SWT程序开发.docVIP

  • 2
  • 0
  • 约2.46千字
  • 约 4页
  • 2020-03-14 发布于河北
  • 举报
一.实验目的及要求 了解SWT的历史 掌握SWT程序开发基本步骤 掌握SWT组件的事件处理机制及方法 掌握SWT中的Label、Text、List、Combo、Table、Tree、Composite、Group、Tab folder、Menus组件的用法。 二、实验内容及步骤 新建一Java工程项目:SWTDemoProject 设置项目的建构路径,右单击项目名,Build Path-Configure Build Path,单击Add External JARs, 定位到C:\Program Files\MyEclipse 6.5\eclipse\plugins, 选中 org.eclipse.swt.win32.win32.x86_3.3.3.v3349.jar 和org.eclipse.swt_3.3.2.v3349d.jar,单击“打开”按钮 在包com.whut中新建一个类LabelDemo: public class LabelDemo { public static void main(String[] args) { //Display连接底层平台和SWT,负责管理SWT事件循环,以及提供访问SWT所需的底层平台资源。 Display display = new Display(); Shell shell = new Shell(display); //shell是程序的主窗口 shell.setText(Hello World); //设置主窗口的标题 shell.setBounds(100, 100, 200, 50); //设置主窗口的大小(长200像 //素,高50像素)和位置(窗口的左角离显示器左角顶点100,100像素处) shell.setLayout(new FillLayout()); //设置主窗口的布局 Label label = new Label(shell, SWT.CENTER); //创建标签组件 label.setText(Hello World); //设置组件的标题 Color red = new Color(display, 255, 0, 0); //创建颜色对象 label.setForeground(red); //改变标签的前景色 shell.open(); //打开主窗口 while (!shell.isDisposed()) { if (!display.readAndDispatch()) //得到事件并且调用对应的监听器进行处理 display.sleep(); //等待事件发生 } red.dispose(); //析构对象red display.dispose(); //析构对象display } } 在包com.whut中新建一个类ButtonDemo: public class ButtonDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText(Button Example); shell.setBounds(100, 100, 200, 100); shell.setLayout(new FillLayout()); final Button button = new Button(shell, SWT.PUSH); button.setText(Click Me Now); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { button.setText(I Was Clicked); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } 在

文档评论(0)

1亿VIP精品文档

相关文档