使用KeyListener的使用Java(Using KeyListener with Java)

2019-10-19 21:38发布

对于一个家庭作业,我需要创建,基本上显示球程序和用户应该能够使用左右键来移动它。 但是,程序没有响应的按键。 我不知道在哪里的错误是,它非常多,如果有人可以帮助我将不胜感激! 这是代码:

public class GraphicsComponent extends JComponent
{
Ellipse2D.Double ball = new Ellipse2D.Double(200, 400, 80, 80);

     public void paintComponent(Graphics g)
     {
         Graphics2D g2 = (Graphics2D) g;
         g2.setColor(Color.RED); 
         g2.fill(ball); 
         g2.draw(ball); 
     }

}


public class BallViewer
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame(); //creates a new JFrame called frame

        frame.setSize(600,600); //invokes the method setSize on the implicit parameter frame
        frame.setTitle("Move this Ball"); //sets the title of the fram
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final GraphicsComponent g = new GraphicsComponent(); //creates a new GraphicsComponent called g, is final so that the inner class can access it
        frame.add(g);//adds component g to the frame

        frame.setVisible(true); //sets the visibility of the frame

        class PressListener implements KeyListener //creates an inner class that implements MouseListener interface
        {
            public void keyPressed(KeyEvent e)
            {

                if (e.getKeyCode() == KeyEvent.VK_LEFT)
                {
                    System.out.println("Left key pressed");
                }

                if (e.getKeyCode() == KeyEvent.VK_RIGHT)
                {
                    System.out.println("Right key pressed");
                }
            }

            public void keyReleased(KeyEvent e)
            {
            }

            public void keyTyped(KeyEvent e)
            {
            }
        }

        PressListener listener = new PressListener(); 
        g.addKeyListener(listener);
    }
}

Answer 1:

KeyListener ,当它注册的组件是可聚焦并拥有可聚焦,将只响应JComponent是不是默认可聚焦。

相反,使用键绑定 ,他们节省您解决有关热点问题搞乱的所有无驱KeyListener

你也将有一个与制造问题Ellipse2D移动,通过设置启动它的位置0x0和平移Graphics上下文的位置,你要画球

举个例子...

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");

am.put("up", new DeltaAction(0, -10));
am.put("down", new DeltaAction(0, 10));
am.put("left", new DeltaAction(-10, 0));
am.put("right", new DeltaAction(10, 0));

DeltaAction ... point是在该位置的Ellipse是画...

public class DeltaAction extends AbstractAction {

    private int deltaX;
    private int deltaY;

    public DeltaAction(int deltaX, int deltaY) {
        this.deltaX = deltaX;
        this.deltaY = deltaY;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        point.x += deltaX;
        point.y += deltaY;

        if (point.x < 0) {
            point.x = 0;
        } else if (point.x + DIAMETER >= getWidth()) {
            point.x = getWidth() - DIAMETER;
        }
        if (point.y < 0) {
            point.y = 0;
        } else if (point.y + DIAMETER >= getHeight()) {
            point.y = getHeight() - DIAMETER;
        }
        repaint();
    }

}


文章来源: Using KeyListener with Java