关于在事件代码中如何访问类中的变量

事件代码访问类中变量的3种方法.

在写事件代码的时候,常常需要引用主类中的变量.要访问这些变量是需要一些技巧的.

方法一:加上final修饰符.

 1 public class HelloWorld5 {
 2     public static void main(String[] args) {
 3         // 在变量前加上final,否则在事件代码里不能引用.
 4         final String str = "kxh";
 5 
 6         Display display = Display.getDefault();
 7         Shell shell = new Shell();
 8         shell.setSize(450, 300);
 9         shell.setText("SWT Application");
10 
11         Text text = new Text(shell, SWT.BORDER);
12         text.setBounds(71, 90, 76, 21);
13         text.addMouseListener(new MouseAdapter() {
14             public void mouseDoubleClick(MouseEvent e) {// 鼠标双击事件的方法
15                 // 打开一个信息框
16                 System.out.println(str);// str变量前要加上final
17                 //如果前面str前不加上final,eclipse就会提示 
18                 //Cannot refer to the non-final local variable str defined in an enclosing scope
19             }
20         });
21         shell.open();
22         shell.layout();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch()) {
25                 display.sleep();
26             }
27         }
28     }
29 }

上面程序的运行结果就是:每次双击文本框的时候都会在控制台打印"kxh"

方法二:将变量str变成类的实例变量.但这种扩大了str变量的有效范围.

 1 public class HelloWorld6 {
 2     // 由于引用它的代码在静态方法内才加上static,否则不必要加上static
 3     static String str = "kxh";
 4     public static void main(String[] args) {
 5 
 6         Display display = Display.getDefault();
 7         Shell shell = new Shell();
 8         shell.setSize(450, 300);
 9         shell.setText("SWT Application");
10 
11         Text text = new Text(shell, SWT.BORDER);
12         text.setBounds(71, 90, 76, 21);
13         text.addMouseListener(new MouseAdapter() {
14             @Override
15             public void mouseDoubleClick(MouseEvent e) {// 鼠标双击事件的方法
16                 // 打开一个信息框
17                 System.out.println(str);// str变量前要加上final
18             }
19         });
20         shell.open();
21         shell.layout();
22         while (!shell.isDisposed()) {
23             if (!display.readAndDispatch()) {
24                 display.sleep();
25             }
26         }
27     }
28 }

运行结果和上面的一样.

方法三:将事件代码写成命名内部类,然后通过构造函数的参数来传入.这种方法比较繁琐.

 1 public class HelloWorld7 {
 2     public static void main(String[] args) {
 3         String str = "kxh";
 4         // 通过构造函数参数将str值传入.
 5         
 6         Display display = Display.getDefault();
 7         Shell shell = new Shell();
 8         shell.setSize(450, 300);
 9         shell.setText("SWT Application");
10 
11         Text text = new Text(shell, SWT.BORDER);
12         text.setBounds(71, 90, 76, 21);
13         text.addMouseListener(new MyMouseDoubleClick(str));
14 
15         shell.open();
16         shell.layout();
17         while (!shell.isDisposed()) {
18             if (!display.readAndDispatch()) {
19                 display.sleep();
20             }
21         }
22     }
23 
24     // 匿名内部类MyMouseDoubleClick
25     public static final class MyMouseDoubleClick extends MouseAdapter {
26         private String string;// 建一个变量引用str的值.
27         public MyMouseDoubleClick(String str) {// 通过构造函数参数接受str值
28             this.string = str;
29         }
30         @Override
31         public void mouseDoubleClick(MouseEvent e) {// 鼠标双击事件的方法
32             System.out.println(string);
33         }
34     }
35 }

 这个方法就在这里做个总结吧,我就不掌握了.......

原文地址:https://www.cnblogs.com/DreamDrive/p/4160301.html