[英]Moving graphics with keylistener/arrow keys
大家好,我正在嘗試使用Java中的keylistener移動帶有箭頭鍵的矩形。 我已完成所有工作並導入了所有內容,但這是說該類名稱未在非抽象類“ SoccerGame”中實現。 我不知道該怎么辦。 我試圖進行許多更改以使其正常運行,但仍無法正常工作。 我還需要導入其他軟件包嗎? 希望你們能提供幫助。 謝謝。
// The "SoccerGame" class.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class SoccerGame extends Applet implements KeyListener
{
int x = 200, y = 200;
public void init ()
{
this.requestFocus ();
addKeyListener (this);
setSize (800, 550);
} // init method
public void paint (Graphics g)
{
g.drawRect (20, 20, 340, 340);
g.fillRect (x, y, 20, 20);
} // paint method
public void keyPressed (KeyEvent e)
{
if (e.getKeyCode()==e.VK_UP)
{
y = y-10;
}
if (e.getKeyCode()==e.VK_DOWN)
{
y = y+10;
}
if (e.getKeyCode()==e.VK_LEFT)
{
x = x-10;
}
if (e.getKeyCode()==e.VK_RIGHT)
{
x = x+10;
}
repaint ();
}
public void keyReleased (KeyEvent e)
{
}
} // SoccerGame class
添加缺少的public void keyTyped(KeyEvent arg0) {}
后,代碼可以正常工作,以避免編譯錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.