import java.awt.*;
import java.awt.event.*;public class Foo
{
public static void main(String[] args)
{
Test t = new Test("匿名类");
t.setSize(200, 200);
t.setVisible(true);
}
}class Test extends Frame
{
private Button button;
public Test(String str)
{
super(str);
//button = new Button("well"); // 执行OK, 没问题
//Button button = new Button("well"); // 为什么这句有问题,local variable button is accessed from within inner class; needs to be declared final
final Button button = new Button("well"); // 为什么声明为 final 类的就能通过
this.add(button, BorderLayout.NORTH);
//TestListener handl = new TestListener(this, button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.red);
button.setLabel("well2");
setBackground(Color.cyan);
}
});
}
}
import java.awt.event.*;public class Foo
{
public static void main(String[] args)
{
Test t = new Test("匿名类");
t.setSize(200, 200);
t.setVisible(true);
}
}class Test extends Frame
{
private Button button;
public Test(String str)
{
super(str);
//button = new Button("well"); // 执行OK, 没问题
//Button button = new Button("well"); // 为什么这句有问题,local variable button is accessed from within inner class; needs to be declared final
final Button button = new Button("well"); // 为什么声明为 final 类的就能通过
this.add(button, BorderLayout.NORTH);
//TestListener handl = new TestListener(this, button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.red);
button.setLabel("well2");
setBackground(Color.cyan);
}
});
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货