0
我想把keylistener和「download.jpg」左右移動。我把;但錯誤說,是必要的。我做錯了什麼?我需要修復什麼?我需要添加任何東西嗎?如何正確添加keylistener
public class GameScreen extends JPanel implements KeyListener {
public BufferedImage bg;
public BufferedImage ci;
public JLabel lab;
int ciXp = 250;
int ciYp = 665;
int ciWidth = 330;
int ciHeight = 745;
public GameScreen()
{
gui();
}
public void gui()
{
try{
bg = ImageIO.read(new File("supermario.jpg"));
ci = ImageIO.read(new File("download.png"));
}
catch(Exception ex){
}
}
public void paintComponent(Graphics g)
{
g.drawImage(bg,0,0,null);
g.drawImage(ci,ciXp,ciYp,ciWidth,ciHeight,0,0,ci.getWidth(),ci.getHeight(),null);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
int ciXp-10;
}
if (key == KeyEvent.VK_RIGHT) {
int ciXp+10;
}
}
}
什麼是你想用'INT ciXp-10來完成;'。此外,你根本沒有在'KeyListener'上實現。 –
我想將下載圖像左移10 .. –
您的'addKeyListener'從代碼 – Blip