【发布时间】:2010-11-23 17:39:56
【问题描述】:
基本上我想要做的是获得一个开始按钮来启动在另一个类中运行并作用于另一个对象的方法。
我的监听器代码:
button1a.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent event) {
// Figure out how to make this work
//sim.runCastleCrash();
}
} );
其他类的代码:
public static void main(String[] args) {
CastleCrash sim;
sim = new CastleCrash();
}
和
public void runCastleCrash() {
System.out.println("Castle Crash is beginning...");
//Other method parts here to be added
}
我觉得这不会太难,但我错过了一块。
【问题讨论】:
-
您遇到了什么错误?当您尝试将 actionListener 添加到按钮时,
sim变量是否还在范围内?一个典型的陷阱是变量必须是 final 才能从匿名内部类(例如 ActionListener)访问。 -
我收到错误:线程“main”中的异常 java.lang.Error:未解决的编译问题:无法解决 sim 我认为您是对的,因为 sim 不在范围内,但我不知道如何使它成为最终的......
-
请看 McDowell 的回答,这就是我要回答的。
标签: java class methods call actionlistener