我有 2 个类用于 pacman 游戏的开始,我对按键侦听器或按键绑定的工作方式感到非常困惑。我有向上、向下、向右和向左移动的方法。我花了几个小时试图弄清楚我,它不会调用常规方法,并且组件让我感到困惑。有人可以帮我看看如何获得箭头键调用方法的最终结果吗?我有下面的代码,但我取出了所有关键测试。
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI {
JPanel myPanel = new JPanel(new GridLayout(50, 50, 1, 1));
int[][] myLabels = new int[50][50];
//Set Game Black
public int[][] setBlack(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
JLabel black = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
myLabels[i][j] = 1;
}
}
return myLabels;
}
//get panel any time changes are made to array
public JPanel getPanel(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
JLabel black = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
JLabel pacman = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
if(myLabels[i][j] == 1){
myPanel.add(black);
}
if(myLabels[i][j] == 2){
myPanel.add(pacman);
}
}
}
return myPanel;
}
//moves pacman up
public int[][] moveUp(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i-1][j] = 2;
}
}
}
return myLabels;
}
//=========
//moves pacman up
public void setLabel(int[][] arrayLabels){
myLabels = arrayLabels;
}
public int[][] moveUpKey(){
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i-1][j] = 2;
}
}
}
return myLabels;
}
//////===========================
//moves pacman down
public int[][] moveDown(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i+1][j] = 2;
}
}
}
return myLabels;
}
//moves pacman right
public int[][] moveRight(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i][j+1] = 2;
}
}
}
return myLabels;
}
//moves pacman left
public int[][] moveLeft(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i][j-1] = 2;
}
}
}
return myLabels;
}
}
游戏类:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.util.*;
public class Game {
public static void main(String[] args) throws Exception {
final GUI gui = new GUI();
JFrame f = new JFrame("Frame Test");
JPanel panel = new JPanel(new GridLayout(50, 50, 1, 1));
int[][] labels = new int[50][50];
JLabel black2 = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
JLabel pacman2 = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
panel.setLayout(new GridLayout(50, 50, 1, 1));
labels = gui.setBlack(labels);
panel = gui.getPanel(labels);
labels[10][10] = 2;
//labels = gui.moveUp(labels);
panel.removeAll();
panel = gui.getPanel(labels);
panel.repaint();
//labels[i][j]
//.revalidate();
//do the thing
f.setContentPane(panel);
f.setSize(1000, 1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}