gpt4 book ai didi

java - 在 JFrame 中添加 numlock 键盘按键的 KeyEvent

转载 作者:行者123 更新时间:2023-11-30 03:36:16 25 4
gpt4 key购买 nike

我使用 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 的每个 numlock 键提供 keylistener 。我该怎么做?谢谢

完整代码:

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

关于java - 在 JFrame 中添加 numlock 键盘按键的 KeyEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27825295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com