我已经使用g.fillRect创建了JFrame和其中的一些矩形,例如:

    Rectangle rec1 = new Rectangle(0, 10, 50, 50);
    g.fillRect((int) rec1.getX(), (int) rec1.getY(), (int) rec1.getWidth(),
            (int) rec1.getHeight());


现在我想知道何时有人按下一些数字锁定键,我尝试使用此代码:

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        y = y - 50;
    }


此代码无法正常工作,我不知道为什么,我需要为1-9中的每个数字锁键添加键侦听器。
我该怎么做?谢谢

完整代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Game extends JComponent {
    private static int y = 0;

    public static void main(String args[]) {
        JFrame mainFrame = new JFrame("BlueTAP Game");
        mainFrame.getContentPane().add(new Game());
        mainFrame.setSize(205, 225);
        mainFrame.setVisible(true);
        mainFrame.setFocusable(true);
        final JFrame parent = new JFrame();
        JOptionPane
                .showMessageDialog(
                        parent,
                        "Shown only one time! - In this game, the buttons are arranged as the keyboard numpad buttons. you are supposed to click on the right keyboard button place by the grey button place.",
                        "Game Instructions", JOptionPane.PLAIN_MESSAGE);
        final Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("H" + y);
            }
        }, 0, 1000);
    }

    public void paint(Graphics g) {

        Rectangle rec1 = new Rectangle(10, 10, 50, 50);
        g.fillRect((int) rec1.getX(), (int) rec1.getY(), (int) rec1.getWidth(),
                (int) rec1.getHeight());
        Rectangle rec2 = new Rectangle(70, 10, 50, 50);
        g.fillRect((int) rec2.getX(), (int) rec2.getY(), (int) rec2.getWidth(),
                (int) rec2.getHeight());
        Rectangle rec3 = new Rectangle(130, 10, 50, 50);
        g.fillRect((int) rec3.getX(), (int) rec3.getY(), (int) rec3.getWidth(),
                (int) rec3.getHeight());
        Rectangle rec4 = new Rectangle(10, 70, 50, 50);
        g.fillRect((int) rec4.getX(), (int) rec4.getY(), (int) rec4.getWidth(),
                (int) rec4.getHeight());
        Rectangle rec5 = new Rectangle(70, 70, 50, 50);
        g.fillRect((int) rec5.getX(), (int) rec5.getY(), (int) rec5.getWidth(),
                (int) rec5.getHeight());
        Rectangle rec6 = new Rectangle(130, 70, 50, 50);
        g.fillRect((int) rec6.getX(), (int) rec6.getY(), (int) rec6.getWidth(),
                (int) rec6.getHeight());
        Rectangle rec7 = new Rectangle(10, 130, 50, 50);
        g.fillRect((int) rec7.getX(), (int) rec7.getY(), (int) rec7.getWidth(),
                (int) rec7.getHeight());
        Rectangle rec8 = new Rectangle(70, 130, 50, 50);
        g.fillRect((int) rec8.getX(), (int) rec8.getY(), (int) rec8.getWidth(),
                (int) rec8.getHeight());
        g.setColor(Color.LIGHT_GRAY);
        Rectangle rec9 = new Rectangle(130, 130, 50, 50);
        g.fillRect((int) rec9.getX(), (int) rec9.getY(), (int) rec9.getWidth(),
                (int) rec9.getHeight());
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_NUMPAD0) {
            y = y - 50;
        }
    }
}

最佳答案

您需要使用数字键盘键值:

VK_NUMPAD0
VK_NUMPAD1
VK_NUMPAD2


...

VK_NUMPAD9

10-07 21:02