事件代码中访问变量有以下注意事项
1、final修饰的局部变量才可以在事件代码中访问
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");
final String name = "tarring.tao";
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println(name);// final的局部变量才能在事件代码中访问
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
2、将局部变量写成类变量后在事件中也可访问
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 static String name = "tarring.tao";
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(name);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
3、将事件写成命名内部类,然后使用构造方法传入参数
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");
String name = "tarring.tao";
button.addSelectionListener(new ButtonSelectionListener(name));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
//外部类实现事件
class ButtonSelectionListener extends SelectionAdapter {
private String name = null;
public ButtonSelectionListener(String name) {
super();
this.name = name;
}
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println(name);
}
}
4、
分享到:
相关推荐
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 核心应用光盘源码下载,解压后即可使用
《Eclipse SWT/Jface核心应用》全面介绍了SWT、JFace和RCP的相关知识。全书共分5篇,第1篇介绍了SWT产生的背景以及SWT的一些基本概念和基础知识。第2篇介绍了SWT基本控件的使用,以及事件处理、布局等SWT基本知识的...
eclipse swt/jface核心应用 这本书的源代码
Eclipse SWT/JFace 核心应用 带书签 Eclipse SWT/JFace
本文档为swt/jface的api中文帮助文档。
Eclipse SWT/JFace核心应用
SWT/JFace开发实例
eclipse swt/jface核心应用源代码 带视频,找了好久,希望可以帮到大家,跟书匹配
SWT/Jface 开发入门指南 介绍以下方面的内容: ? 设定 swt 以及 JFace 的开发环境 ? swt 的一些简单部件(widget)介绍... swt 和 JFace 的事件模式 ? 通过一些简单的例子说明如何利用 swt 和 JFace 编写图形化应用程序
这个是SWT3.4版本的HTML格式的API
Eclipse_Swt_Jface_核心应用
王占全,苏玲 著; ISBN:7-121-05152-4 学习 SWT/JFace 的好书!
《Eclipse_Swt_Jface_核心应用》介绍Eclipse的核心,详细讲解了SWT,JFace,RCP,介绍了SWT,JFace的基本控件以及事件处理,布局,及SWT的高级应用,JFace的框架及RCP的框架及应用的开发.这里是这本书的光盘源码
NULL 博文链接:https://niub.iteye.com/blog/1806580