gpt4 book ai didi

Java 键适配器键事件错误...需要分号而不是括号?

转载 作者:行者123 更新时间:2023-12-01 16:32:38 24 4
gpt4 key购买 nike

我正在遵循与关键事件有关的编码教程,所有代码看起来都正确,但由于某种原因,有一个我无法修复的错误。也许我需要的只是另一双眼睛来扫描它并发现我的错误。这是代码。有错误的代码行两侧各有 2 个星号。该错误告诉我,这是一个“;” (分号)是预期的,其中“(”和“)”是......这有什么意义?我也会发一张照片。

package com.Bench3.myGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Keying extends JPanel{

public Rectangle character;

public int charW = 24;
public int charH = 36;

public boolean right = false;
public boolean left = false;

public Keying(Display f, Images i){
character = new Rectangle(180, 180, charW, charH);

f.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_D){
right = true;
}
if(e.getKeyCode() == KeyEvent.VK_A){
left = true;
}

**public void keyReleased(KeyEvent e)**{
if(e.getKeyCode() == KeyEvent.VK_D){
right = false;
}
if(e.getKeyCode() == KeyEvent.VK_A){
left = false;
}
}
}
});
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.BLACK);
g.setColor(Color.WHITE);
g.fillRect(character.x, character.y, character.width, character.height);

if(right){
character.x += 1;
}
if(left){
character.x -= 1;
}
repaint();
}

} Here's the error up close. hope it helps THANK YOU !

最佳答案

您忘记关闭上一个方法的大括号(keyPressed()):-

public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_D){
right = true;
}
if(e.getKeyCode() == KeyEvent.VK_A){
left = true;
}
} // missing

关于Java 键适配器键事件错误...需要分号而不是括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12985594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com