首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MouseListener和KeyPressed

MouseListener和KeyPressed
EN

Stack Overflow用户
提问于 2012-10-06 10:59:04
回答 1查看 324关注 0票数 1

我已经使用KeyListenerActionListenerJFrame中构建了一个简单的动画程序,它可以根据按下的箭头键(类似于snake)将一个框移动到一个方向。但我注意到,如果我启动应用程序并移动鼠标,应用程序将不会继续检查按下了哪个箭头键以及移动到哪个方向。

有人能给我解释一下吗?这是因为我需要禁用一些涉及鼠标事件的东西吗?

代码如下:

代码语言:javascript
复制
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);

    }

}

EN

回答 1

Stack Overflow用户

发布于 2012-10-06 23:59:38

我不知道你有什么问题。我用了你的代码,没有任何问题。

下面是我使用的代码:

代码语言:javascript
复制
        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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12756303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档