使用KeyListener与Java [英] Using KeyListener with Java

查看:154
本文介绍了使用KeyListener与Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public class GraphicsComponent extends JComponent 
{
Ellipse2D.Double ball = new Ellipse2D (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(); //创建一个名为框架的新JFrame

frame.setSize(600,600); //调用隐式参数框架
frame.setTitle(Move this Ball)的方法setSize; //设置框架的标题
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final GraphicsComponent g = new GraphicsComponent(); //创建一个名为g的GraphicsComponent,是最终的,以便内部类可以访问它
frame.add(g); //将组件g添加到框架

frame.setVisible真正); //设置框架的可见性

class PressListener实现KeyListener //创建一个实现MouseListener接口的内部类
{
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);
}
}


解决方案

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

而$ code> 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();
}

}


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

}

这篇关于使用KeyListener与Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆