一、匿名内部类写法
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从入门到精通》 陈刚 著 读书笔记