【问题标题】:The image not showing up in JFrame after adding the movement添加运动后图像未显示在 JFrame 中
【发布时间】:2021-02-12 23:20:32
【问题描述】:

我正在尝试用老虎追逐百吉饼来替代 PacMan(不要问为什么)。我还在第一阶段,试图让老虎在 JFrame 周围移动。但是,现在我实现了 KeyEvent,图像不再显示。我已经坚持了一个小时,我不明白我哪里出错了。

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

主要:

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);
            }

        }
    });
}

用户界面:

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;
}

【问题讨论】:

标签: java swing jlabel keylistener keyevent


【解决方案1】:

您的 UI 类不完整,因此我无法准确说出您在做什么。我只能猜测。

忽略实际的 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 类“是”JPanel,您将 KeyListener 添加到其中。
  2. 然后您创建第二个 JPanel 并将“PacMan”添加到该面板并将该面板添加到框架中。

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

你的班级应该是这样的:

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

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

【讨论】:

  • 很高兴它有帮助。不要忘记通过单击复选标记(答案旁边)来“接受”答案,这样人们就知道问题已经解决了。见:stackoverflow.com/help/accepted-answer
【解决方案2】:

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

UI类代码:

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());

}

【讨论】:

  • 我发现我的 keyReleased 函数从未被调用过 - 我给了你原因。原因是您有两个面板并将 KeyListener 添加到错误的面板中。 在 UI 方法中移动 KeyListener - 完全没有必要。是否将 KeyListener 定义为匿名内部类或是否在类中实现 KeyListener 是无关紧要的。相关的是您将 KeyListener 添加到的组件。
猜你喜欢
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多