需要让keyListener监听,而不管Java的重点是什么

需要让keyListener监听,而不管Java的重点是什么,java,keylistener,jcreator,Java,Keylistener,Jcreator,我想做一个刽子手游戏。我一直在努力听键盘敲击声。我希望用户能够按下一个键,它将进行测试,看看它是否正确。我不想把它输入到一个文本框中,不管它的焦点是什么,我想让那个键与这个词进行对比测试。我试着在面板上添加一个keylistener,但它不起作用。整个东西由这个面板、一个左面板、一个主面板和一个主框架组成 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public cl

我想做一个刽子手游戏。我一直在努力听键盘敲击声。我希望用户能够按下一个键,它将进行测试,看看它是否正确。我不想把它输入到一个文本框中,不管它的焦点是什么,我想让那个键与这个词进行对比测试。我试着在面板上添加一个keylistener,但它不起作用。整个东西由这个面板、一个左面板、一个主面板和一个主框架组成

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class RightPanel extends JPanel
{
JLabel jlbMissed, jlbWord, jlbScore, jlbTimer;
JComboBox jcbDifficulty;
JButton jbtStart, jbtQuit;
String[] difficulties = {"Easy", "Medium", "Hard"};
String[] words = {"First", "Next", "Hello", "World"};
char incorrectChar, correctChar;
String word;
int currentScore;
boolean clockIsRunning = false;
boolean gameInPlay = false;
int sec = 0;
int min = 0;




public RightPanel()
{

    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    ActionHandler actionHandler = new ActionHandler();
    jlbMissed   = new JLabel("Missed: " + incorrectChar);
    jlbWord     = new JLabel("Word: " + correctChar);
    jlbScore    = new JLabel("Score: " + currentScore);
    jlbTimer    = new JLabel("Time: " + "0:00");
    jbtStart    = new JButton("Start");
    jbtQuit     = new JButton("Quit");
    jcbDifficulty = new JComboBox();
    for (int i = 0; i < 3; i++)
    {
        jcbDifficulty.addItem(difficulties[i]); // Creates Difficutly ComboBox
    }
    this.add(jcbDifficulty, getConstraints(0,0,1,1, GridBagConstraints.NORTH));
    this.add(jlbMissed, getConstraints(0,1,1,1, GridBagConstraints.CENTER));
    this.add(jlbWord, getConstraints(0,2,1,1, GridBagConstraints.CENTER));
    this.add(jlbScore, getConstraints(0,3,1,1, GridBagConstraints.CENTER));
    this.add(jlbTimer, getConstraints(0,4,1,1, GridBagConstraints.CENTER));
    this.add(jbtStart, getConstraints(0,6,1,1, GridBagConstraints.CENTER));
    this.add(jbtQuit, getConstraints(1,6,1,1, GridBagConstraints.CENTER));
    jbtStart.addActionListener(actionHandler);
    jbtQuit.addActionListener(actionHandler);
    Random ran = new Random();  //
    int rand = ran.nextInt(4);  // Generates random number then selects word from words array
    word = words[rand];         //
    KeyListener k = new KeyAdapter()
    {
         public void keyPressed(KeyEvent e) {System.out.println("key was pressed");}
    };
    this.addKeyListener(k);




}




private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor)
{
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5,5,5,5);
    c.ipadx = 0;
    c.ipady = 0;
    c.gridx = gridx;
    c.gridy = gridy;
    c.gridwidth = gridwidth;
    c.gridheight = gridheight;
    c.anchor = anchor;
    return c;
}

class ActionHandler implements ActionListener
  {
public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if (source == jbtStart)
    {
        clockIsRunning = true;
        MyTimer timer = new MyTimer();
        timer.start();
        gameInPlay = true;
    }
    else if (source == jbtQuit)
    {
        System.exit(0);
        }

}
  }
  class MyTimer extends Thread
  {
public void run()
{
    while(true)
    {
        if(!clockIsRunning)
            break;
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException ecp)
        {
        }

        if (sec == 59)
        {
            min++;
            sec = 0;
        }
        else
            sec++;
        if(sec < 10)
            jlbTimer.setText("Time:" + min+":0"+sec);
        else
            jlbTimer.setText("Time:" + min+":"+sec);
    }
}
  }
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
公共类RightPanel扩展了JPanel
{
JLabel jlbimessed、jlbWord、jlbcore、jlbTimer;
JComboBox JCB困难;
JButton jbtStart、jbtguit;
字符串[]困难={“容易”,“中等”,“难”};
String[]words={“First”、“Next”、“Hello”、“World”};
字符不正确字符,正确字符;
字符串字;
int-currentScore;
布尔值clockIsRunning=false;
布尔gameInPlay=false;
整数秒=0;
int min=0;
公共权利小组()
{
this.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
ActionHandler ActionHandler=新的ActionHandler();
jlbMissed=新JLabel(“Missed:+不正确字符”);
jlbWord=新JLabel(“单词:+correctChar”);
jlbScore=新的JLabel(“分数:+currentScore”);
jlbTimer=新的JLabel(“时间:“+”0:00”);
jbtStart=新的JButton(“开始”);
jbtQuit=新的JButton(“退出”);
jcbDifficulty=newjcombobox();
对于(int i=0;i<3;i++)
{
jcbDifficulty.addItem(困难[i]);//创建困难组合框
}
add(jcbDifficulty,getConstraints(0,0,1,1,GridBagConstraints.NORTH));
add(jlbMissed,getConstraints(0,1,1,GridBagConstraints.CENTER));
add(jlbWord,getConstraints(0,2,1,1,GridBagConstraints.CENTER));
add(jlbcore,getConstraints(0,3,1,1,GridBagConstraints.CENTER));
add(jlbTimer,getConstraints(0,4,1,1,gridbagsconstraints.CENTER));
add(jbtStart,getConstraints(0,6,1,1,GridBagConstraints.CENTER));
add(jbtQuit,getConstraints(1,6,1,1,GridBagConstraints.CENTER));
addActionListener(actionHandler);
addActionListener(actionHandler);
Random ran=新的Random()//
int rand=ran.nextInt(4);//生成随机数,然后从单词数组中选择单词
单词=单词[兰德]//
KeyListener k=新的KeyAdapter()
{
public void keyPressed(KeyEvent e){System.out.println(“键被按下”);}
};
这个.addKeyListener(k);
}
私有GridBagConstraints getConstraints(int-gridx、int-gridy、int-gridwidth、int-gridheight、int-anchor)
{
GridBagConstraints c=新的GridBagConstraints();
c、 插图=新插图(5,5,5,5);
c、 ipadx=0;
c、 ipady=0;
c、 gridx=gridx;
c、 gridy=gridy;
c、 gridwidth=gridwidth;
c、 网格高度=网格高度;
c、 锚=锚;
返回c;
}
类ActionHandler实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
对象源=e.getSource();
if(source==jbtStart)
{
clockIsRunning=真;
MyTimer=新的MyTimer();
timer.start();
gameInPlay=true;
}
else if(source==jbtquiit)
{
系统出口(0);
}
}
}
类MyTimer扩展线程
{
公开募捐
{
while(true)
{
如果(!时钟正在运行)
打破
尝试
{
睡眠(1000);
}
捕获(中断异常ecp)
{
}
如果(秒=59)
{
min++;
秒=0;
}
其他的
sec++;
如果(第10节)
jlbTimer.setText(“时间:+min+”:0“+sec);
其他的
jlbTimer.setText(“时间:+min+:”+sec);
}
}
}

如果我理解正确,您希望此关键侦听器可以随时随地工作。如果是这样,您只需将框架设置为自己的
关键侦听器,如中所述

我知道它看起来需要一个文本框,但请尝试将其添加为第118行:

displayArea.addKeyListener(this);

我创建了显示区域“jTeTaReaDePosialField:”现在我的118行在定时器方法的中间显示出来,我把它放在右边的KeyListener(的底部),它在Java.AWT中出现了消息AddikListIdter(java. AWT.Evr..java),不能应用到(右面板)中。@马库斯,不,不是你的第118行。我不知道你的程序中的行号是什么;你甚至没有全部发布。如果你在我链接的演示中把它作为第118行添加,即使输出区域有焦点,它也能工作。它只起作用,因为displayArea.addKeyListener会监听。如果焦点在按钮上,那么它就不起作用。现在我只是简单地说向组合框和按钮添加KeyListener,因为它们几乎是唯一可以聚焦的东西