1

とを使用して、押された矢印キー(ヘビに似ています)に応じた方向にボックスを移動する簡単なアニメーションプログラムをJFrame作成KeyListenerActionListenerました。しかし、アプリケーションを起動してマウスを動かすと、アプリケーションはどの矢印キーが押されたか、どの方向に移動するかをチェックし続けないことに気づきました。

誰かが私にこれを説明できますか、それは私がマウスイベントを含む何かを無効にする必要があるためですか?

ここにコードがあります:

public class gui extends JPanel implements ActionListener, KeyListener{
    Timer tm = new Timer(5, this);
    int x = 300, y = 178, velx = 0, vely = 0;


    public gui() {
        tm.start();
        addKeyListener(this);
        setFocusable(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(x, y, 50, 30);

    }

    @Override
    public void keyPressed(KeyEvent e) {
        keys(e);
    }

    public void keys(KeyEvent e) {
        int c = e.getKeyCode();
        if (c == KeyEvent.VK_LEFT)

        {
            velx = -1;
            vely = 0;
        }
        if (c == KeyEvent.VK_UP)

        {
            velx = 0;
            vely = -1;
        }
        if (c == KeyEvent.VK_RIGHT)

        {
            velx = 1;
            vely = 0;
        }
        if (c == KeyEvent.VK_DOWN)

        {
            velx = 0;
            vely = 1;

        }
    }

    public void borders(ActionEvent e) {
        if (x < 0) {
            velx = 0;
            x = 0;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);
        }
        if (x > 530) {
            velx = 0;
            x = 530;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);
        }
        if (y < 0) {
            velx = 0;
            y = 0;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);
        }
        if (y > 330) {
            velx = 0;
            y = 330;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);

        }

    }

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

    }


    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    public void actionPerformed(ActionEvent e) {
        x += velx;
        y += vely;
        repaint();
        borders(e);

    }

}

4

1 に答える 1

0

あなたの問題が何であるかわかりません。私はあなたのコードを使用しましたが、問題はありませんでした。

これが私が使用したものです:

        public gui() 
        {
            addKeyListener(this);
            setFocusable(true);
        }

        public void keyPressed(KeyEvent e)
        {
            int c = e.getKeyCode();
            if (c == KeyEvent.VK_LEFT)
            {
                velx = -1;
                vely = 0;
            }
            if (c == KeyEvent.VK_UP)
            {
                velx = 0;
                vely = -1;
            }
            if (c == KeyEvent.VK_RIGHT)
            {
                velx = 1;
                vely = 0;
            }
            if (c == KeyEvent.VK_DOWN)
            {
                velx = 0;
                vely = 1;
            }
        }

        public void actionPerformed(ActionEvent e)
        {
            x += velx;
            y += vely;
            repaint();
            borders(e);
        }

        public static void main(String[] args)
        {
            JFrame frame = new JFrame("gui");
            frame.add(new gui());
            frame.setVisible(true);
            frame.setSize(600, 400);
        }
    }

プログラムを開始する前にマウスをクリックして動かし、次にキーを使用しました。それは正常に動作します。ただし、KeyBindings に切り替えます。

于 2012-10-06T15:59:38.247 に答える