`
tarring
  • 浏览: 14557 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

SWT/JFace_003_在事件代码中访问变量

阅读更多

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

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、

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics