实现移动 为什么不能移动
package lxh.Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class huaziji {
JFrame JF = new JFrame();
JPanel JP = new JPanel();
huabi hb = new huabi(10,10);
public void huamian(){
JF.setLocation(100,100);
JF.setSize(1000,1000);
JF.setVisible(true);
JF.add(hb);
JF.addKeyListener(new mypanel1());
}
@SuppressWarnings("serial")
class huabi extends JPanel{
private int x;
private int y;
public huabi(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.drawOval(10,10,50,50);
}
}
class mypanel1 extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e){
super.keyPressed(e);
if(e.getKeyCode()==KeyEvent.VK_UP){
hb.y--;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN){
hb.y++;
}
else if(e.getKeyCode() ==KeyEvent.VK_LEFT){
hb.x--;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
hb.x++;
}
hb.repaint();
}
}
public static void main(String args[])
{
huaziji hz = new huaziji();
hz.huamian();
}
}
------解决方案--------------------我试过了,可以移动