gpt4 book ai didi

Java如何制作Keylistener?

转载 作者:行者123 更新时间:2023-11-29 03:26:26 24 4
gpt4 key购买 nike

我正在尝试为我制作的游戏制作高分屏幕,我想使用 keylistener,但没有任何反应。

这是我的代码中需要修复的部分。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import java.io.*;

public class score extends JPanel {
public static char highchar[] = new char[10];
public static int i = 0;
public static boolean flag = true;

public static void scr() {
JFrame f = new JFrame("HighScores");
score c = new score();
f.setContentPane(c);
f.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize()
.getWidth() - 13) / 2 - 200, (int) (Toolkit.getDefaultToolkit()
.getScreenSize().getHeight() - 63) / 2 - 100);
c.setPreferredSize(new Dimension(400, 200));
f.pack();
f.setResizable(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane();
f.setVisible(true);
while (flag) {
c.repaint();
}
}

public score() {
addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {
System.out.println("hello");
if (flag) {
if (e.getKeyCode() == KeyEvent.VK_A) {
highchar[i] = 'A';
}
if (e.getKeyCode() == KeyEvent.VK_B) {
highchar[i] = 'B';
}
if (e.getKeyCode() == KeyEvent.VK_C) {
highchar[i] = 'C';
}
if (e.getKeyCode() == KeyEvent.VK_D) {
highchar[i] = 'D';
}
if (e.getKeyCode() == KeyEvent.VK_E) {
highchar[i] = 'E';
}
if (e.getKeyCode() == KeyEvent.VK_F) {
highchar[i] = 'F';
}
if (e.getKeyCode() == KeyEvent.VK_G) {
highchar[i] = 'G';
}
if (e.getKeyCode() == KeyEvent.VK_H) {
highchar[i] = 'H';
}
if (e.getKeyCode() == KeyEvent.VK_I) {
highchar[i] = 'I';
}
if (e.getKeyCode() == KeyEvent.VK_J) {
highchar[i] = 'J';
}
if (e.getKeyCode() == KeyEvent.VK_K) {
highchar[i] = 'K';
}
if (e.getKeyCode() == KeyEvent.VK_L) {
highchar[i] = 'L';
}
if (e.getKeyCode() == KeyEvent.VK_M) {
highchar[i] = 'M';
}
if (e.getKeyCode() == KeyEvent.VK_N) {
highchar[i] = 'N';
}
if (e.getKeyCode() == KeyEvent.VK_O) {
highchar[i] = 'O';
}
if (e.getKeyCode() == KeyEvent.VK_P) {
highchar[i] = 'P';
}
if (e.getKeyCode() == KeyEvent.VK_Q) {
highchar[i] = 'Q';
}
if (e.getKeyCode() == KeyEvent.VK_R) {
highchar[i] = 'R';
}
if (e.getKeyCode() == KeyEvent.VK_S) {
highchar[i] = 'S';
}
if (e.getKeyCode() == KeyEvent.VK_T) {
highchar[i] = 'T';
}
if (e.getKeyCode() == KeyEvent.VK_U) {
highchar[i] = 'U';
}
if (e.getKeyCode() == KeyEvent.VK_V) {
highchar[i] = 'V';
}
if (e.getKeyCode() == KeyEvent.VK_W) {
highchar[i] = 'W';
}
if (e.getKeyCode() == KeyEvent.VK_X) {
highchar[i] = 'X';
}
if (e.getKeyCode() == KeyEvent.VK_Y) {
highchar[i] = 'Y';
}
if (e.getKeyCode() == KeyEvent.VK_Z) {
highchar[i] = 'Z';
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
highchar[i] = ' ';
}
if (e.getKeyCode() == KeyEvent.VK_0) {
highchar[i] = '0';
}
if (e.getKeyCode() == KeyEvent.VK_1) {
highchar[i] = '1';
}
if (e.getKeyCode() == KeyEvent.VK_2) {
highchar[i] = '2';
}
if (e.getKeyCode() == KeyEvent.VK_3) {
highchar[i] = '3';
}
if (e.getKeyCode() == KeyEvent.VK_4) {
highchar[i] = '4';
}
if (e.getKeyCode() == KeyEvent.VK_5) {
highchar[i] = '5';
}
if (e.getKeyCode() == KeyEvent.VK_6) {
highchar[i] = '6';
}
if (e.getKeyCode() == KeyEvent.VK_7) {
highchar[i] = '7';
}
if (e.getKeyCode() == KeyEvent.VK_8) {
highchar[i] = '8';
}
if (e.getKeyCode() == KeyEvent.VK_9) {
highchar[i] = '9';
}
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
i--;
highchar[i] = ' ';
i--;
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
flag = false;
}
i++;
System.out.println(highchar[i]);
}
}
});
}
}

能否在不更改 scr 方法的情况下修复 keylistener?

最佳答案

如果您进行过任何搜索,您会发现 KeyListeners 仅在具有焦点的组件上工作,这对于 JPanel 意味着使其可聚焦,someJPanel.setFocusable(true) 然后将焦点分配给它 someJPanel.requestFocusInWindow()

您还会看到,如果可能的话,通常要避免使用 KeyListener,以支持 Key Bindings。 .

作为旁注,这让我担心:

while(flag){
c.repaint();
}

在 Swing GUI 中执行 while true 循环是一件危险的事情,因为您会占用事件线程。不要这样做——没有理由这样做。

我看到它是您的 scr 方法的一部分,并且您已经声明,

Can you please fix the keylistener without changing the scr method?

scr方法是你的导师给你的吗?如果是这样,我不会太相信那个导师。

关于Java如何制作Keylistener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768592/

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