keylistener java,使用KeyListener与Java

For a homework assignment I need to create a program that essentially displays a ball and the user should be able to move it using the left and right keys. However, the program isn't responding to the keys. I don't know where the bug is, and I'd appreciate it very much if someone could help! This is the code:

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);

}

}

解决方案

KeyListener will only respond when the component it's registered is focusable AND has focusable, JComponent is not focusable by default.

Instead, use key bindings, they save you all the hassel of messing with focus issues related to KeyListener

You're also going to have problems with making the Ellipse2D move, start by setting it's position as 0x0 and translating the Graphics context to the position you want to paint the ball

As an example...

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));

And the DeltaAction...point is the location at which the Ellipse is painted...

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值