KeyListener即使看似正确也不工作

时间:2015-03-22 17:06:53

标签: java android swing

在处理我的程序时,我发现由于某种原因,我的KeyPressed方法没有做任何事情。这是代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel implements KeyListener{

    Environment environment = new Environment();
    Cube cube = new Cube();



    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        environment.createBox(g2d);
        cube.createCube(g2d);

    }


    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
            cube.cube.setLocation(cube.x + 200, cube.y);
        }
    }

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

    }

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

    }




    public static void main(String[] args) throws InterruptedException {

        JFrame frame = new JFrame();


        Main m = new Main();

        m.addKeyListener(m);

        frame.add(m);

        frame.setSize(700, 1000);
        frame.setVisible(true);
        frame.setTitle("The Cube");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);

        frame.setBackground(new Color(240, 84, 84));

        while (true) {
            m.repaint();
            Thread.sleep(10);
        }

    }
}

我真的很感激帮助,因为我现在已经找了一个星期而且找不到解决方案。

3 个答案:

答案 0 :(得分:0)

这可能是因为您的面板是focusable。尝试添加m.setFocusable(true),然后点击面板。然后它应该拦截关键事件。

答案 1 :(得分:0)

在上面创建一个构造函数:

Main()
{
    addKeyListener(this);
}

答案 2 :(得分:0)

我设法到达某个地方,然而,立方体似乎只是变大而不是移动。 以下是两个类:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import java.awt.RenderingHints;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Main extends JPanel implements KeyListener {

        Environment environment = new Environment();
        Cube cube = new Cube();

        private int x = 200;
        private int y = 620;

        public Main() {
            addKeyListener(this);
        }

        public void paint(Graphics g) {

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

            environment.createBox(g2d);
            cube.createCube(g2d, x, y);

        }

        public void moveCube() {
            x = x + 1;
            y = y + 1;
        }

        @Override
        public void keyPressed(KeyEvent e) {

            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                cube.cube.setLocation(x = x + 3, y = y);
            }
        }

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

        }

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

        }

        public static void main(String[] args) throws InterruptedException {

            JFrame frame = new JFrame();

            Main m = new Main();
            m.setFocusable(true);

            frame.add(m);

            frame.setSize(700, 1000);
            frame.setVisible(true);
            frame.setTitle("The Cube");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(true);
            frame.setLocationRelativeTo(null);

            frame.setBackground(new Color(240, 84, 84));

            while (true) {

                m.repaint();
                Thread.sleep(3);
            }

        }
    }

    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

    public class Cube {

        public Rectangle cube;


        public void createCube(Graphics2D g2d, int x, int y){

            cube = new Rectangle(100, 800, x, y);

            g2d.setColor(new Color(148, 235, 148));

            g2d.fill(cube);

        }


    }

感谢您的帮助! :)