簡體   English   中英

在Java中使用KeyListener

[英]Using KeyListener with Java

對於家庭作業,我需要創建一個實質上顯示一個球的程序,用戶應該能夠使用左右鍵移動它。 但是,該程序沒有響應鍵。 我不知道該錯誤在哪里,如果有人可以提供幫助,我將不勝感激! 這是代碼:

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僅在其注冊的組件可聚焦且具有可聚焦時響應,而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();
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM