局部类?
import java.awt.*;
import java.awt.event.*;
public class TestKeyboardListener {
public static void main(String[] args){
new TestKeyboardListener();
}
TestKeyboardListener (){
Frame f = new Frame("KeyBoardListener");
f.setBounds(100,100,200,300);
f.setVisible(true);
f.addKeyListener(new KeyAdapterMonitor());
class KeyAdapterMonitor extends KeyAdapter{
private int up,down;
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP)
System.out.println("UP");
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
System.out.println("down");
}
}
}
}
显示错误:KeyAdapterMonitor cannot be resolved to a type
请问在方法内定义的类在方法中不能使用么?还是有别的什么说道?我要是把KeyAdapterMonitor类定义在方法外就可以正常运行了 求解释
------解决方案--------------------把
f.addKeyListener(new KeyAdapterMonitor());
这一句放到定义KeyAdapterMonitor的后面即可