在我的刽子手应用程序中,我为用户创建了一组按钮,然后从文件中随机选择在刽子手应用程序中猜测的单词...使用 JLabel 打印单词...但是只要一个按钮单击应用程序冻结。谁能告诉我这正在发生...
我是如何创建按钮的
for(char i = 'A'; i <= 'Z'; i++){
String buttonText = new Character(i).toString();
JButton button = getButton(buttonText);
panel1.add(button);
}
按钮动作监听器
public JButton getButton(final String text){
final JButton button = new JButton(text);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(original.toUpperCase().indexOf(button.getText())!=-1){
guessString = text;
guessLetter = guessString.charAt(0);
StringBuilder builder = new StringBuilder(secret);
while(error < 6){
}
for (int i = 0; i < original.length(); i++){
if (original.charAt(i) == guessLetter){
builder.setCharAt(i, guessLetter);
}
}
secret = builder.toString();
}
else{
JOptionPane.showMessageDialog(null, "There is no " + text );
error++;
if(error >= 0) imageName = "hangman1.jpg";
if(error >= 1) imageName = "hangman2.jpg";
if(error >= 2) imageName = "hangman3.jpg";
if(error >= 3) imageName = "hangman4.jpg";
if(error >= 4) imageName = "hangman5.jpg";
if(error >= 5) imageName = "hangman6.jpg";
if(error >= 7) imageName = "hangman7.jpg";
}
}
});
return button;
}