无论 Java 的焦点是什么,都需要有 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);
}
}
}
Im trying to make a hangman game. Im stuck on trying to listen for the keystroke. I want the user to be able to press a key and it will test to see if it is correct or not. I dont want to have to have it inputted into a text box, regardless of focus I want that key to be tested against the word. Ive tried adding a keylistener to the panel and it doesnt work. The whole thing consists of this panel, a leftpanel, a main panel and then a main frame.
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您希望这个关键监听器随时随地工作。如果是这种情况,您可以将框架设为自己的
KeyListener
,如 此演示来自 Swing 教程。我知道它看起来需要一个文本框,但尝试将其添加为第 118 行:
If I understand you correctly, you want this key listener to work everywhere, all the time. If that's the case, you can just make your frame its own
KeyListener
, as described in this demo from the Swing tutorial.I know it looks like it requires a text box, but try adding this as line 118: