import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Windows extends JFrame{
static int m=80;
static int x;
static int n=80;
static char c;
int i=0;
public Windows(){
super("sage");
setSize(600,600);
setVisible(true);
addKeyListener(this.new Keylisten());
}
public void paint(Graphics g){
g.setColor(Color.blue);
for(int i=0;i<60;i++){
g.drawLine(0,10*i,600,10*i);
g.drawLine(10*i,0,10*i,600);
}
g.setColor(Color.red);
while(true){
if(i==0)
g.fillRect(80,80,10,10);
switch(this.c){
case 'w':{g.fillRect(m,n-10*i,10,10);
break;}
case 'd':{g.fillRect(m+10*i,n,10,10);
break;}
case 's':{g.fillRect(m,n+10*i,10,10);
break;}
case 'a':{g.fillRect(m-10*i,n,10,10);
break;}
default:break;
}
try{Thread.sleep(200);
}catch(Exception e){}
i++;
}
}
class Keylisten extends KeyAdapter{
public void keyPressed(KeyEvent e){
Windows.c=e.getKeyChar();
repaint();
}
}
}
public class picture{
public static void main(String[] args){
new Windows();
}
}
请大家指教 这里有错误吗 ? 我想监听W,D,S,A键执行上面的SWITCH()但是办不倒 请高手指教 谢谢!!