代码的以下部分不能工作,因为赢/输计数对每个单词的增量大于1,有时我得到字符串长度的nullpointerexception。此外,尽管玩家应该得到7次尝试,但有时他会得到更多,有时会更少。字符串取自文本文件"Hangeng.txt"。整个游戏是在一个键盘键入的监听器中,这个监听器是在一个按钮监听器中。任何关于游戏布局应该如何安排以避免错误的建议都是受欢迎的,因为我才刚刚开始使用swing和gui的东西。
public class test{
static int won = 0;
static int lost = 0;
static String key = "";
static String word = null;
static int no = 0;
static StringBuffer toguess;
public static void main(String[] args) throws IOException{
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(3,1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JButton button = new JButton();
JLabel label = new JLabel();
JLabel label2 = new JLabel();
panel1.add(label);
panel2.add(button);
panel3.add(label2);
frame.setSize(800,600);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.setVisible(true);
//the button that starts the game or gets a new word
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.requestFocus();
no = 0;
label2.setText("won " + won + ", lost " + lost);
button.setText("Next");
//get random word from file
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(
"hangeng.txt"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
int lineno = (int) (Math.random() * 100);
for (int i = 0; i < lineno; i++) {
try {
reader.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
word = reader.readLine().replace(" ", "");
} catch (IOException e1) {
e1.printStackTrace();
}
String missing = "";
for (int u = 0; u < (word.length() - 2); u++) {
missing = missing + "*";
}
final String guess = word.charAt(0) + missing
+ word.charAt((word.length() - 1));
toguess = new StringBuffer(guess);
label.setText(toguess.toString());
final ArrayList<String> tried = new ArrayList<String>();
//keylistener that listens to key clicks by the user
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent arg0) {
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
key = "" + arg0.getKeyChar();
String guessing = null;
boolean k = false;
if ((no < 6)) {
guessing = key;
System.out.println(guessing);
if (!(tried.contains(guessing))) {
tried.add(guessing);
for (int length = 1; length < (guess
.length() - 1); length++) {
if (guessing.equals(String.valueOf(word.charAt(length)))) {
toguess.replace(length,
(length + 1),
String.valueOf(word.charAt(length)));
k = true;
}
}
if (k == true) {
label.setText(toguess.toString());
} else {
no = no + 1;
}
k = false;
}
label.setText(toguess.toString());
if (toguess.toString().equals(word)) {
label.setText("Correct! The word was " + word);
no = 6;
won = won + 1;
}
}
else if ((no == 6)
&& (!(toguess.toString().equals(word)))) {
label.setText("Sorry, but the word was " + word);
lost = lost + 1;
}
}
});
}
});
}
}
+1到所有的评论....
添加到它们:
-
不要使用
KeyListener
,使用KeyAdapter
,但是如果您使用Swing而不是AWT,则应该使用KeyBinding
s用于Swing,例如 -
不要忘记通过
SwingUtiltities.invokeLater(..)
块在Event Dispatch Thread
上创建和操作Swing组件。 -
检查类命名方案,它们应该以大写字母开头,即
test
应该是Test
,之后的每个新单词都应该大写。 -
不要在
JFrame
上调用setSize
,而是使用适当的LayoutManager
和/或覆盖JPanel
的getPreferredSize()
,并返回适合其内容的大小,并在添加所有组件后在JFrame
实例上调用pack()
。 -
另外SSCCE应该从复制粘贴中编译,这不是....例如,变量需要更改为final,我没有Hangeng.txt的样本,因此无法测试
-
最后使用@Override注释来确保你重写了正确的方法,即
@Override public void actionPerformed(ActionEvent e) { }