刽子手的照片没有变化

时间:2012-08-28 00:42:36

标签: java swing

我正在忙着编写一个刽子手应用程序,我正在检查一些代码是否有效...现在我还没有隐藏单词部分,所以在代码的位置我使用了if语句作为补充代码:

 if(original.indexOf(button.getText())!=-1){
                 JOptionPane.showMessageDialog(null, "Your word does contain" + text );
             }
             else{
                 JOptionPane.showMessageDialog(null, "There is no" + text );
                 error++;
             }
             }

无论如何,当我按下不在单词中的按钮时,它会根据

添加到我的错误中
error++;

它只找到单词中的第一个字母。我的一句话是恐龙,当我按D时它说“是的,有一个D”但是当我按A时它说“不,没有我”,其中显然是

有人可以帮忙吗

这是我的完整代码

import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;

public final class Hangman extends JFrame implements ActionListener{
String original = readWord();
int error = 0;
String imageName;

JButton btnAddWord = new JButton("Add New Word");
JButton btnRestart = new JButton("Restart");
JButton btnHelp = new JButton("Help");
JButton btnExit = new JButton("Exit");

JLabel word = new JLabel(original);

static JPanel panel1 = new JPanel();
static JPanel panel2 = new JPanel();
static JPanel panel3 = new JPanel();
static JPanel panel4 = new JPanel();

public Hangman(){
    Container content =getContentPane();
    content.setLayout(new GridLayout(0,1));

   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";
    ImageIcon icon = null;
    if(imageName != null){
        icon = new ImageIcon(imageName);
    }
   JLabel image = new JLabel();
   image.setIcon(icon);

   btnAddWord.addActionListener(this);
   btnRestart.addActionListener(this);
   btnHelp.addActionListener(this);
   btnExit.addActionListener(this);

   panel2.add(image);
   panel3.add(word);
   panel4.add(btnAddWord);
   panel4.add(btnRestart);
   panel4.add(btnHelp);
   panel4.add(btnExit);

    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.indexOf(button.getText())!=-1){
                 JOptionPane.showMessageDialog(null, "Your word does contain " + text );
             }
             else{
                 JOptionPane.showMessageDialog(null, "There is no " + text );
                 error++;
             }
             }
             });
             return button;
}
public String readWord(){
    try{
        BufferedReader reader = new BufferedReader(new FileReader("Words.txt"));
        String line = reader.readLine();
        List<String> words = new ArrayList<String>();
        while(line != null){
            String[] wordsLine = line.split(" ");
            boolean addAll = words.addAll(Arrays.asList(wordsLine));
            line = reader.readLine();
        }
        Random rand = new Random(System.currentTimeMillis());
        String randomWord = words.get(rand.nextInt(words.size()));
        return randomWord;

}catch (Exception e){
    return null;
}
}
public void actionPerformed(ActionEvent e){
    if(e.getSource() == btnAddWord){
        try{
            FileWriter fw = new FileWriter("Words.txt", true);
            PrintWriter pw = new PrintWriter(fw, true);

            String word = JOptionPane.showInputDialog("Please enter a word: ");

            pw.println(word);
            pw.close();
        }
        catch(IOException ie){
            System.out.println("Error Thrown" + ie.getMessage());
        }
    }
    if(e.getSource() == btnRestart){

    }
    if(e.getSource() == btnHelp){
        String message = "The word to guess is represented by a row of dashes, giving the number of letters and category of the word."
               + "\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions."
               + "\nIf the suggested letter does not occur in the word, the other player draws one element of the hangman diagram as a tally mark."
               + "\n"
               + "\nThe game is over when:"
               + "\nThe guessing player completes the word, or guesses the whole word correctly"
               + "\nThe other player completes the diagram";
       JOptionPane.showMessageDialog(null,message, "Help",JOptionPane.INFORMATION_MESSAGE);
    }
    if(e.getSource() == btnExit){
        System.exit(0);
    }
}

public static void main (String [] args){
    Hangman frame = new Hangman();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 600);
    frame.add(panel1, BorderLayout.NORTH);
    frame.add(panel2, BorderLayout.CENTER);
    frame.add(panel3, BorderLayout.SOUTH);
    frame.add(panel4, BorderLayout.SOUTH);
}
}

2 个答案:

答案 0 :(得分:3)

我的第一个猜测是你的文本比较区分大小写。

"Dinosaur".indexOf("A")"Dinosaur".indexOf("a")

不同

我建议您在比较时将文本转换为常见情况。

original.toLowerCase().indexOf(button.getText().toLowerCase())!=-1

答案 1 :(得分:3)

这是因为您没有正确检查您从该文件中读取的单词是否与JButton中的文本相同。有几种方法可以解决这个问题:

  1. 正如@MadProgrammer建议的那样。进行几项检查以涵盖小写和大写字符。

  2. Standardizing您的List<String> words,即在您从文件中读取时将所有内容保存在一个案例中。这样你就不用担心检查它是小写还是大写。因此,在这种情况下,您可能需要更改:String[] wordsLine = line.split(" ");String[] wordsLine = line.toLowerCase().split(" ");String[] wordsLine = line.toUpperCase().split(" ");取决于您对哪个感到满意)。然后使用单个indexOf()操作进行检查就可以了。