---
title: "SWT/JFace_003_在事件代码中访问变量"
author: "tarring"
date: "2010-09-03T19:27:38Z"
tags: []
categories: ["编程语言"]
slug: "swt-jface-003"
draft: false
lastmod: "2010-09-03T19:27:38Z"
id: 754871
url: "https://tarring.iteye.com/blog/754871"
---

事件代码中访问变量有以下注意事项

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、