所以我一直在尝试做一些简单的编码:用 WASD 键移动一个蓝色方块,
public static void main (String args[])
{
new MoveWASD();
}
public MoveWASD()
{
super("Use-WASD-to-Move");
setSize(800, 450);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, 800, 450);
g.setColor(Color.BLUE);
g.fillRect(Location[0], Location[1], 20, 20);
System.out.println("PRINTEDs");
}
public class MoveDetection extends Thread implements KeyListener
{
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent arg0)
{
public void run()
{
while(true)
{
try
{
if(event.getKeyChar() == 'w')
{
Location[1] = Location[1] - 20;
Repeat = true;
}
else if(event.getKeyChar() == 'd')
{
Location[0] = Location[0] + 20;
}
else if(event.getKeyChar() == 's')
{
Location[1] = Location[1] + 20;
}
else if (event.getKeyChar() == 'a')
{
Location[0] = Location[0] - 20;
}
else
{
Location[0] = Location[0];
}
Thread.sleep(75);
}
catch(Exception e)
{
break;
}
}
}
}
}
但是,将静态空洞放入另一个时会出错,箭头(上方)指向我的两个错误:一个运行不足,表示:令牌“void”上的语法错误,@预期和另一个在结束括号下在 run( )中显示:语法错误,插入“EnumBody”以完成 BlockStatements 另外,当我在 movedetection 类下的任何“void”下放置“System.out.write()”时,它不会显示,这可能意味着它是'没有得到它......因为我刚刚开始编码,我不知道这意味着什么,有什么帮助吗?