在JFrame中为numlock键盘键添加KeyEvent

时间:2015-01-07 17:39:57

标签: java jframe

我使用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());

现在我想知道当有人按下某些numlock键时,我尝试使用此代码:

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

此代码无效,我不知道为什么,我需要 keylistener 来支持1-9的每个 numlock 键。 我该怎么做? THX

完整代码:

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

1 个答案:

答案 0 :(得分:2)

您需要使用小键盘键值:

VK_NUMPAD0
VK_NUMPAD1
VK_NUMPAD2

...

VK_NUMPAD9