为什么按右键播放器就不能移动?

h5qlskok  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(238)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

昨天关门了。
改进这个问题
首先,我为我的英语道歉,但我会尽我所能描述这个问题
我想按右箭头键移动播放器。到目前为止还不错,但没动。它只是改变了x值的值。
以下是我的代码的一部分:

public class Draw extends JLabel {
    private Gui gui;
    public Rect Player;

    public Draw(Gui gui,int width,int height){
        this.gui = gui;
        createDefaultSettings(width, height);
    }

    private void createDefaultSettings(int width,int height ){
        gui.width = width;
        gui.height = height;
        setBounds(0, 0, width, height);
        setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Player = new Rect(g,50,50,50,50);
        Player.paint(g);
        repaint();
    }
}

public class KeyHandler implements KeyListener {

    public Draw Maindraw;

    public KeyHandler(Draw Maindraw) {
        this.Maindraw = Maindraw;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            Maindraw.Player.x += 5;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
}

public class Rect extends JLabel{

    public int x, y, width, height;

    public Rect(Graphics g,int x,int y,int width,int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        g.drawRect(x, y, width, height);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawRect(x, y, width, height);
    }
}

请帮忙,谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题