|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
package com.msb;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
|
import java.awt.event.WindowAdapter;
|
|
|
|
|
import java.awt.event.WindowEvent;
|
|
|
|
|
|
|
|
|
@ -16,10 +18,12 @@ public class TankFrame extends Frame {
|
|
|
|
|
private int y = 200;
|
|
|
|
|
|
|
|
|
|
public TankFrame() {
|
|
|
|
|
|
|
|
|
|
setTitle("Tank war");
|
|
|
|
|
setSize(800, 600);
|
|
|
|
|
setResizable(true);
|
|
|
|
|
setVisible(true);
|
|
|
|
|
|
|
|
|
|
addWindowListener(new WindowAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void windowClosing(WindowEvent e) {
|
|
|
|
@ -27,11 +31,31 @@ public class TankFrame extends Frame {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
addKeyListener(new MyKeyListener());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
|
super.paint(g);
|
|
|
|
|
g.fillRect(x, y, 50, 50);
|
|
|
|
|
x += 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//这里使用内部内的原因是,只有TankFrame用到
|
|
|
|
|
class MyKeyListener extends KeyAdapter {
|
|
|
|
|
@Override
|
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
|
super.keyPressed(e);
|
|
|
|
|
/*x += 10;
|
|
|
|
|
repaint();*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
|
super.keyReleased(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|