一、匿名内部类写法
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("hello");
// 匿名内部类实现事件
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("点击了hello按钮...");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
private Shell shell = null;
private Button button = null;
private void createShell() {
shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
button = new Button(shell, SWT.NONE);
button.setText("hello");
// 匿名内部类方式实现事件
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
System.out.println("widgetSelected()");
}
});
}
public static void main(String[] args) {
Display display = Display.getDefault();
HelloWorld thisClass = new HelloWorld();
thisClass.createShell();
thisClass.shell.open();
while (!thisClass.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
二、命名内部类写法
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("hello");
button.addSelectionListener(new ButtonSelectionListener());
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
// 命名内部类实现事件
private static class ButtonSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("点击了hello按钮...");
}
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
private Shell shell = null;
private Button button = null;
private void createShell() {
shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
button = new Button(shell, SWT.NONE);
button.setText("hello");
button.addSelectionListener(new ButtonSelectionListener());
}
// 命名内部类方式实现事件
private class ButtonSelectionListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent e) {
System.out.println("widgetSelected()");
}
}
public static void main(String[] args) {
Display display = Display.getDefault();
HelloWorld thisClass = new HelloWorld();
thisClass.createShell();
thisClass.shell.open();
while (!thisClass.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
三、外部类写法
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("hello");
button.addSelectionListener(new ButtonSelectionListener());
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
// 外部类实现事件
class ButtonSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("点击了hello按钮...");
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
private Shell shell = null;
private Button button = null;
private void createShell() {
shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
button = new Button(shell, SWT.NONE);
button.setText("hello");
// 外部类方式实现事件
button.addSelectionListener(new ButtonSelectionListener());
}
public static void main(String[] args) {
Display display = Display.getDefault();
HelloWorld thisClass = new HelloWorld();
thisClass.createShell();
thisClass.shell.open();
while (!thisClass.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
//外部类实现事件
class ButtonSelectionListener extends SelectionAdapter {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("widgetSelected()");
}
}
四、实现监听接口的写法
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld extends SelectionAdapter {// 或者实现SelectionListener接口
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("hello");
button.addSelectionListener(new HelloWorld());
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("点击了hello按钮...");
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld extends SelectionAdapter {// 或者实现SelectionListener接口
private Shell shell = null;
private Button button = null;
private void createShell() {
shell = new Shell();
shell.setText("第一个SWT程序");
shell.setSize(new Point(300, 200));
shell.setLayout(new GridLayout());
button = new Button(shell, SWT.NONE);
button.setText("hello");
button.addSelectionListener(new HelloWorld());
}
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("widgetSelected()");
}
public static void main(String[] args) {
Display display = Display.getDefault();
HelloWorld thisClass = new HelloWorld();
thisClass.createShell();
thisClass.shell.open();
while (!thisClass.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
《ECLIPSE从入门到精通》 陈刚 著 读书笔记
分享到:
相关推荐
Java程序设计之swt教程,SWT/Jface
SWT/Jface API 3.4 SWT/Jface API 3.4 SWT/Jface API 3.4 SWT/Jface API 3.4
SWT/JFACE API,是关于SWT/JFACE开发所用的API
swt/Jface用到的最基本的jar包
Eclipse SWT/JFace SWT JFace 实例 Eclipse SWT/JFace 书中 全部实例
《Eclipse SWT/Jface核心应用》全面介绍了SWT、JFace和RCP的相关知识。全书共分5篇,第1篇介绍了SWT产生的背景以及SWT的一些基本概念和基础知识。第2篇介绍了SWT基本控件的使用,以及事件处理、布局等SWT基本知识的...
Eclipse SWT/JFace 核心应用光盘源码下载,解压后即可使用
Eclipse SWT/JFace 核心应用 带书签 Eclipse SWT/JFace
Eclipse SWT/JFace核心应用
这个是SWT3.4版本的HTML格式的API
SWT/JFace开发实例
SWT/Jface 开发入门指南 介绍以下方面的内容: ? 设定 swt 以及 JFace 的开发环境 ? swt 的一些简单部件(widget)介绍... swt 和 JFace 的事件模式 ? 通过一些简单的例子说明如何利用 swt 和 JFace 编写图形化应用程序
本文档为swt/jface的api中文帮助文档。
Eclipse_Swt_Jface_核心应用
王占全,苏玲 著; ISBN:7-121-05152-4 学习 SWT/JFace 的好书!
eclipse swt/jface核心应用 这本书的源代码
eclipse swt/jface核心应用源代码 带视频,找了好久,希望可以帮到大家,跟书匹配
《Eclipse_Swt_Jface_核心应用》介绍Eclipse的核心,详细讲解了SWT,JFace,RCP,介绍了SWT,JFace的基本控件以及事件处理,布局,及SWT的高级应用,JFace的框架及RCP的框架及应用的开发.这里是这本书的光盘源码
NULL 博文链接:https://niub.iteye.com/blog/1806580