KeyListener和ActionListener未注册。

huangapple go评论67阅读模式
英文:

KeyListener and ActionListener not registering

问题

以下是您提供的Java代码的翻译部分:

class Platformer:

    import java.awt.Dimension;
    import java.awt.Toolkit;
    import javax.swing.JFrame;

    public class Platformer {
	
	    public static void main(String[] args) {
		
	        MainFrame frame = new MainFrame();
	        frame.setSize(700,700);
	        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	        frame.setLocationRelativeTo(null);
	        frame.setResizable(false);
	        frame.setTitle("Platformer");
	        frame.setVisible(true);
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    }
    }
    
class GamePanel:

// ...(GamePanel 类的代码)

class Player:

// ...(Player 类的代码)

class KeyChecker:

// ...(KeyChecker 类的代码)

class Wall:

// ...(Wall 类的代码)

class MainFrame:

// ...(MainFrame 类的代码)

希望这对您有所帮助。如果您有任何其他问题,请随时提问。

英文:

I have been working on a Java project that makes a character move with the keys, but the keyListener is not working and registering my actions. I code in eclipse, and I think that problem is my GamePanel, Player, or me KeyChecker class. I am using a guide to help start this, so it is the same code as another project, but it does not work. Here is my code:

class Platformer:

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Platformer {

    public static void main(String[] args) {
	
        MainFrame frame = new MainFrame();
        frame.setSize(700,700);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setTitle("Platformer");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class GamePanel:
I have tried changing the layout of the if statements if that was the original problem, but it did not change anything. I also tried changing the statements to just void instead of public void but that did not work either.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements ActionListener{

	Player player;
	ArrayList<Wall> walls = new ArrayList<>();
	Timer gameTimer;
	
	public GamePanel() {
		
		player = new Player(400, 300, this);
		
		makeWalls();
		
		gameTimer = new Timer();
		gameTimer.schedule(new TimerTask() {

			public void run() {
				player.set();
				repaint();	
			}
			
		},0,17);
	}

	public void makeWalls() {
		for (int i = 50; i < 650; i += 50) {
			walls.add (new Wall(i, 600, 50, 50));
		}
		walls.add(new Wall(50, 550, 50, 50));
		walls.add(new Wall(50, 500, 50, 50));
		walls.add(new Wall(50, 450, 50, 50));
		walls.add(new Wall(600, 550, 50, 50));
		walls.add(new Wall(600, 500, 50, 50));
		walls.add(new Wall(600, 450, 50, 50));
		walls.add(new Wall(450, 550, 50, 50));

	}
	
	public void paint(Graphics g) {
		
		super.paint(g);
		Graphics2D gtd = (Graphics2D) g;
		player.draw(gtd);
		for (Wall wall: walls) wall.draw(gtd);
	}
	
	public void KeyPressed(KeyEvent e) {
		if (e.getKeyChar() == 'a')player.keyLeft = true;
		if (e.getKeyChar() == 'w') player.keyUp = true;
		if (e.getKeyChar() == 's') player.keyDown = true;
		if (e.getKeyChar() == 'd') player.keyRight = true;
	}
	
	public void KeyReleased(KeyEvent e) {
		if (e.getKeyChar() == 'a') player.keyLeft = false;
		if (e.getKeyChar() == 'w') player.keyUp = false;
		if (e.getKeyChar() == 's') player.keyDown = false;
		if (e.getKeyChar() == 'd') player.keyRight = false;
	}
	
	public void actionPerformed(ActionEvent e) {
	}
	
}

class Player:
I also tried the same things that I did for the GamePanel, but that did not work either.

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Player {
	
	GamePanel panel;
	int x;
	int y;
	int width;
	int height;
	
	double xspeed;
	double yspeed;
	
	Rectangle hitBox;
	
	boolean keyRight;
	boolean keyLeft;
	boolean keyUp;
	boolean keyDown;
	
	public Player(int x, int y, GamePanel panel) {
		
		this.panel = panel;
		
		this.x = x;
		this.y = y;
		
		width = 50;
		height = 100;
		hitBox = new Rectangle(x,y,width,height);
	}
	
	public void set() {
	
		if (keyLeft && keyRight || !keyLeft && !keyRight) xspeed *= 0.8;
		else if (keyLeft && !keyRight) xspeed --;
		else if (keyRight && !keyLeft) xspeed ++;
		
		if (xspeed > 0 && xspeed < 0.75) xspeed = 0;
		if (xspeed < 0 && xspeed > - 0.75) xspeed = 0;
		
		if (xspeed > 7) xspeed = 7;
		if (xspeed < -7) xspeed = -7;
		
		if (keyUp) {
			
			hitBox.y++;
			for(Wall wall: panel.walls) {
				if (wall.hitBox.intersects(hitBox)) yspeed = -6;
			}
			hitBox.y --;			
		}
		
		yspeed += .3;
		
		hitBox.x += xspeed;
		for(Wall wall: panel.walls) {
			if (hitBox.intersects(wall.hitBox)) {
				hitBox.x-= xspeed;
				while(!wall.hitBox.intersects(hitBox)) hitBox.x += Math.signum(xspeed);
				hitBox.x -= Math.signum(xspeed);
				xspeed = 0;
				x = hitBox.x;
			}
		}
		
		hitBox.y += xspeed;
		for(Wall wall: panel.walls) {
			if (hitBox.intersects(wall.hitBox)) {
				hitBox.y-= yspeed;
				while(!wall.hitBox.intersects(hitBox)) hitBox.y += Math.signum(yspeed);
				hitBox.y -= Math.signum(yspeed);
				yspeed = 0;
				y = hitBox.y;
			}
		}
		
		x += xspeed;
		y += yspeed;
		
		hitBox.x = x;
		hitBox.y = y;		
	}
	
	public void draw(Graphics2D gtd) {
		gtd.setColor(Color.BLACK);
		gtd.fillRect(x, y, width, height);
		
	}	
}

class KeyChecker:

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyChecker extends KeyAdapter{
	
	GamePanel panel;
	
	public KeyChecker(GamePanel panel) {
		this.panel = panel;
	}
	
	public void KeyPressed(KeyEvent e) {
		panel.KeyPressed(e);
	}
	
	public void KeyReleased(KeyEvent e) {
		panel.KeyReleased (e);
	}
}

class Wall:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Wall {
	int x;
	int y;
	int width;
	int height;
	
	Rectangle hitBox;
	
	public Wall (int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = width;
		
		hitBox = new Rectangle(x, y, width, height);
	}
	
	public void draw(Graphics2D gtd) {
		gtd.setColor(Color.BLACK);
		gtd.drawRect(x, y, width, height);
		gtd.setColor(Color.WHITE);
		gtd.fillRect(x + 1, y + 1, width - 2, height - 2);
	}
}

class MainFrame:

import java.awt.Color;
import javax.swing.JFrame;

public class MainFrame extends JFrame{
	
	public MainFrame() {
		
		GamePanel panel = new GamePanel();
	
		panel.setLocation(0,0);
		panel.setSize(this.getSize());
		panel.setBackground(Color.LIGHT_GRAY);
		panel.setVisible(true);
		this.add(panel);
		
		addKeyListener(new KeyChecker(panel));
	}
}

Thank you for your help.

答案1

得分: 1

以下是翻译好的部分:

KeyChecker 中的函数应该以小写字母开头,因此是 public void keyPressed 和 keyReleased。
此外,您应该在 KeyChecker 和 GamePanel 类中添加 implements KeyListener(从 GamePanel 中删除 ActionListener)。
然后,添加未实现的函数。Eclipse 将会询问您。

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

备注:最好是使用 @Override 注解覆盖现有的 keyPressed 和 keyReleased 函数,而不是创建新函数。

我尝试过了,它有效!

英文:

The functions in KeyChecker should start with lower case, so public void keyPressed and keyReleased.
In addition, you should add implements KeyListener to the classes KeyChecker and GamePanel (remove ActionListener in GamePanel).
Then, add the unimplemented functions. Eclipse will ask you for it.

@Override
public void keyPressed(KeyEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void keyReleased(KeyEvent e) {
	// TODO Auto-generated method stub
	
}

Remark, it's better to @Override the functions keyPressed and keyReleased, rather than creating new functions.

I tried them, it worked!

huangapple
  • 本文由 发表于 2020年10月8日 03:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64251481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定