事件代码中访问变量有以下注意事项
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、