添加运动后图像未显示在 JFrame 中

The image not showing up in JFrame after adding the movement

我正在尝试用老虎追百吉饼制作吃豆人替代品(不要问为什么)。我仍处于第一阶段,试图让老虎在 JFrame 周围移动。但是,既然我实施了 KeyEvent,图像就不再显示了。卡在这一个小时了,不明白哪里错了

编辑:我已经显示了图像,但是当按下箭头键时图像没有更新或改变位置,可能与 KeyEvent 和 PacMan 之间的连接有关 class .

主要:

public Main() {

}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {


            UI frame = null;
            try {
                frame = new UI();
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(-1);
            }

        }
    });
}

UI:

public PacMan PacMan;

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_RIGHT){
        PacMan.moveRight();
    }
    if (key == KeyEvent.VK_LEFT){
        PacMan.moveLeft();
    }
    if (key == KeyEvent.VK_UP){
        PacMan.moveUp();
    }
    if (key == KeyEvent.VK_DOWN){
        PacMan.moveDown();
    }
}
public UI() throws IOException {
    this.PacMan = new PacMan();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    frame.setTitle("PacMan");
    frame.setResizable(false);
    frame.setSize(1200, 700);
    frame.setMinimumSize(new Dimension(1200, 700));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setBackground(Color.BLACK);
    panel.add(PacMan.getImage());
    frame.add(panel);
    frame.setVisible(true);
}

吃豆人:

public int xCoords = 570;
public int yCoords = 320;
JLabel pacManImage = new JLabel();
Icon tigerLeft;
Icon tigerRight;
public PacMan() throws IOException {

    ImageIcon tigerLeft = new ImageIcon(new ImageIcon("textures/tigerLeft.png").getImage().getScaledInstance(60, 40, Image.SCALE_DEFAULT));
    ImageIcon tigerRight = new ImageIcon(new ImageIcon("textures/tigerRight.png").getImage().getScaledInstance(60, 40, Image.SCALE_DEFAULT));

    pacManImage.setIcon(tigerRight);

    pacManImage.setVisible(true);
}

public void initialDraw() {
    pacManImage.setBounds(xCoords, yCoords, 60, 40);
    pacManImage.setIcon(tigerRight);
    pacManImage.repaint();

}

public void moveRight() {
    System.out.println("here: " + tigerRight);
    //xCoords = xCoords + 2;
    pacManImage.setIcon(tigerRight);
    pacManImage.setLocation(pacManImage.getLocationOnScreen().x + 2, pacManImage.getLocationOnScreen().y);
    pacManImage.repaint();
}


public void moveLeft() {
    //xCoords = xCoords + 2;
    pacManImage.setIcon(tigerLeft);
    pacManImage.setLocation(pacManImage.getLocationOnScreen().x - 2, pacManImage.getLocationOnScreen().y);
    pacManImage.repaint();
}

public void moveUp() {
    //yCoords = yCoords + 2;
    pacManImage.setLocation(pacManImage.getLocationOnScreen().x, pacManImage.getLocationOnScreen().y - 2);
    pacManImage.repaint();
}

public void moveDown() {
    //yCoords = yCoords + 2;
    pacManImage.setLocation(pacManImage.getLocationOnScreen().x, pacManImage.getLocationOnScreen().y + 2);
    pacManImage.repaint();
}
public JLabel getImage(){
    return pacManImage;
}

您的 UI class 不完整,所以我无法准确判断您在做什么。我只能猜测。

忽略实际的 KeyListener 代码,我猜你有这样的代码:

public class UI extends JPanel
{
    public UI() throws IOException 
    {
        this.PacMan = new PacMan();
        addKeyListener(this);
    
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
    
        panel.setBackground(Color.BLACK);
        panel.add(PacMan.getImage());
        frame.add(panel);
        frame.setVisible(true);
    }
}

所以您又一次拥有两个 JPanel 组件:

  1. UI class“是一个”JPanel,您将 KeyListener 添加到它。
  2. 然后创建第二个 JPanel 并将“PacMan”添加到该面板并将该面板添加到框架。

所以问题是第一个面板有 KeyListener 但它从未添加到框架中。

您的 class 应如下所示:

public class UI extends JPanel
{
    public UI() throws IOException 
    {
        this.PacMan = new PacMan();
        addKeyListener(this);
    
        setBackground(Color.BLACK);
        add(PacMan.getImage());
    }
}

就是这样。框架的创建不属于这个class.

我发现我的 keyReleased 函数从未被调用,我通过发布最简单的修复解决了这个问题,在 UI 方法中移动 KeyListener。

UIclass代码:

public class UI extends JPanel {
public PacMan PacMan;


public UI() throws IOException {
    this.PacMan = new PacMan();
    addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN){
                PacMan.moveDown();
            }
            if (e.getKeyCode() == KeyEvent.VK_UP){
                PacMan.moveUp();
            }
            if (e.getKeyCode() == KeyEvent.VK_LEFT){
                PacMan.moveLeft();
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT){
                PacMan.moveRight();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {}

        @Override
        public void keyTyped(KeyEvent e) {}
    });
    setFocusable(true);
    setBackground(Color.BLACK);
    add(PacMan.getImage());

}