SWT/AWT的问题:怎样在listener中访问其他控件?

rainy14f 2003-09-15 11:27:28
照着例子写的,还会出错.....请高手指点!!

原代码:
Display display = new Display ();
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.CENTER);
...
shell.addControlListener (new ControlAdapter () {
public void controlResized (ControlEvent e) {
label.setBounds (shell.getClientArea ());
}
});

出错信息:
java.lang.Error: Unresolved compilation problem:
Cannot refer to a non-final variable label inside an inner class defined in a different method

...全文
54 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mars_wx 2003-09-16
  • 打赏
  • 举报
回复
你的这些代码是不是写在STATIC方法内的?如果是你的LABALE要定义成FINAL
noratong 2003-09-16
  • 打赏
  • 举报
回复
我给你个简单的代码:
这是第一种方法:

public class myframe extends Frame
{
……
TextFiled txt;
Button bon;
……
myframe()
{
……
txt = new TextFiled(10);
bon = new Button("OK");
bon.addActionListener(action);
};
……
}
class action implements ActionListener
{
TextFiled text;
action(TextFiled t)
{
text = t;
}
public void AcitonPerformed(ActionEvent e)
{
text.setText("Ok!");
}
}

这是第二种方法:
public class myframe extends Frame
{
……
TextFiled txt;
Button bon;
……
myframe()
{
……
txt = new TextFiled(10);
bon = new Button("OK");
bon.addActionListener(action);
};
……
class action implements ActionListener //这个就称为内部类!
{
public void AcitonPerformed(ActionEvent e)
{
txt.setText("Ok!");
}
}
}
rainy14f 2003-09-16
  • 打赏
  • 举报
回复
明白了,各位大佬,多谢了!!
gcdead 2003-09-15
  • 打赏
  • 举报
回复
label.setBounds (shell.getClientArea ());
rainy14f 2003-09-15
  • 打赏
  • 举报
回复
ding!
rainy14f 2003-09-15
  • 打赏
  • 举报
回复
还不是很明白,具体怎么实现呢? 什么是内部类??
还有,怎么能把控件当参数传进来,要写新类继承原来的监听类吗?
noratong 2003-09-15
  • 打赏
  • 举报
回复
想在监听器内调用其它控件,一种是把这个控件当参数传递进来;另一种是把监听器写成内部类,那么这个监听器所在的类中的所有控件,监听器都能访问到了。
rainy14f 2003-09-15
  • 打赏
  • 举报
回复
其他的也没几行,主要的代码就是这些了!
是不是我必须要把label声明为final啊?
duoshanx 2003-09-15
  • 打赏
  • 举报
回复
就不舍的把代码全发出来吗?
^_^
rainy14f 2003-09-15
  • 打赏
  • 举报
回复
ding a !
rainy14f 2003-09-15
  • 打赏
  • 举报
回复
up!

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧