アプレットを起動すると、キー リスナーから応答がありません。これを修正するにはどうすればよいですか? 以下はコードです。
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
public class Main extends Applet {
private static final long serialVersionUID = 1L;
public String mode = "";
public Graphic gr;
public void init() {
this.setSize(400, 400);
gr = new Graphic();
this.add(gr);
gr.addKeyListener(new MyKeyListener());
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
gr.requestFocus();
gr.repaint();
}
};
new Timer().scheduleAtFixedRate(timerTask, 0, 1000 / 5);
}
public void paint(Graphics g) {
}
private class MyKeyListener extends KeyAdapter {
public MyKeyListener(){
System.out.println("HELLO");
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
gr.direction = Graphic.Direction.up;
gr.move();
break;
case KeyEvent.VK_DOWN:
gr.direction = Graphic.Direction.down;
gr.move();
break;
case KeyEvent.VK_LEFT:
gr.direction = Graphic.Direction.left;
gr.move();
break;
case KeyEvent.VK_RIGHT:
gr.direction = Graphic.Direction.right;
gr.move();
break;
}
}
public void keyReleased(KeyEvent e) {
}
}
}