Hey i was wondering if someone could have a look at this and help me figure out why the output is repeating it's self and how I would fix it. I have spent several hours trying to fix this. Thanks in advance.
/* Author :
* Date :02/10/10
* Title: Hangman
*/
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.Color;
import java.awt.Font;
public class Hangman implements ActionListener {
JButton button;
JTextArea text = new JTextArea();
JTextField guess;
JLabel AlphaGuess = new JLabel();
JTextField RandomWord;
JLabel RandomWordDisplay = new JLabel();
JLabel Result = new JLabel();
String Wordcollection[] = new String[] {"plasma", "chicken", "mouse"}; //Array of words
char LetterInput [] = new char [10];
String Length = "";
String Randomword;
char LetterGen;
public static void main(String[] args)
{
Hangman gui = new Hangman();
gui.go();
}
public void go()
{
JFrame frame = new JFrame("Hangman"); //The programs name is Hangman
JPanel panel = new JPanel();
JLabel label = new JLabel("Guess a letter for the word : ");
guess = new JTextField(20);
button = new JButton("Solve!");
button.addActionListener(this);
//************ This section of code makes the title display in a different format *************
Font font = new Font("Verdana", Font.BOLD, 16);
text.setFont(font);
text.setForeground(Color.RED);
JLabel text = new JLabel("\n \n H _ n g m _ n");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//*********** This section of code displays the order of which things are displayed on my panel.
panel.add(text);
panel.add(label);
panel.add(guess);
panel.add(button);
panel.add(AlphaGuess);
panel.add(Result);
//************ This section of code gives the length and width of the program *****************
frame.getContentPane().add(panel);
frame.setSize(300,300);
frame.setVisible(true);
//************ This section generates and hides a random word *********************************
Random Number = new Random(); //Random Number is produced
int Word = Number.nextInt(Wordcollection.length); //value is assigned to Word
Randomword = Wordcollection[Word]; // will go to the certain point in the words array
int WordLength = Randomword.length()+1; //Word length is is assigned
for(int i=1;i <WordLength; i++)
{
RandomWordDisplay.setText(Length = Length + '-'); //will display each letter as a - on the panel
}
}
// ************** This method allocates what each button should do ********************************
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button)
{
Process();
}
}
// ************ This method switches the _ with a letter if correct *******************************
public void Process()
{
String Guess = guess.getText();
for (int i=0; i<Randomword.length(); i++)
{
if (Randomword.charAt(i) == Guess.charAt(0))
{
if (LetterInput [i] != '-')
LetterInput [i] = Guess.charAt(0);
Result.setText("1. Congratualtions you have the correct letter " + Guess);
}
else
{
if (LetterInput [i] == '-')
LetterInput [i] = '-';
Result.setText("2. You have entered an incorrect letter " + Guess);
}
if (Guess.length()>1)
{
Result.setText("Please only enter one letter!");
}
}
for (int i=0; i<Randomword.length(); i++)
{
Length = Length + Character.toString(LetterInput [i]);
}
{
AlphaGuess.setText(Length);
}
}
}