1 Attachment(s)
JOption to JFrame HELP...messed my program up
hi everyone,
i am creating a word guess game for my degree and instead of reading the assignment brief properly i created it in jopton panes and had it working 110%, HOWEVER this was short lived until last week until i found out that it had to be in JFrames.
I thought it would be an easy fix but really it isnt once i got going.
I didnt pay much attention to creating jframes because i was to busy thinking i was getting ahead creating it in joption panes and now i am really stuck.
If someone could have a look at this code for me, if someone can tell me why it runs but when the start button is clicked on it doesnt work that would be a great help or even show me a quick example piece of code that i could change mine to because i honestly dont know what i am doing with JFrames.
Attachment 916
i cant thank anyone enough for helping
Re: JOption to JFrame HELP...messed my program up
Please post your code in the forum that you want looked at.
Re: JOption to JFrame HELP...messed my program up
hi, thanks for replying.
my code is in my first post in a text file
sorry i am kind of new to the forum but i attached it in a text file and inserted it to my first post
Re: JOption to JFrame HELP...messed my program up
Code :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
public class WordGuessGame
{
JFrame frame;
JPanel panel;
JLabel label;
JTextArea input;
String word;
JButton checkletter;
JButton giveup;
public static void main(String[]args)
{
WordGuessGame app = new WordGuessGame();
app.start();
}
public void start()
{
frame = new JFrame();
panel = new JPanel();
frame.getContentPane().add(BorderLayout.NORTH, panel);
frame.setVisible(true);
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new FlowLayout());
label = new JLabel(" Word Guessing Game");
panel.add(label);
checkletter = new JButton("Start");
panel.add(checkletter);
input = new JTextArea("");
panel.add(input);
giveup = new JButton("Give up!");
panel.add(giveup);
}
//create an inner listener class for the first button
class startListener implements ActionListener
{
String guess;
int wrongGuess;
Random generator = new Random();
JLabel invalidLbl;
JLabel guessedLbl;
public void actionPerformed(ActionEvent event)
{
//string array of words
String [] words = {"paprika", "savvy", "knickknack", "cyclists",
"daydreamt", "cushion", "karaoke", "parlour", "cormac",
"riverrock", "storage", "chemistry", "cockatoo", "fujitsu"};
char userGuess, again;//declare variables
//start do loop
do {
int num = words.length;//words that are available
word = selectRandomWord(words, num);//selects a random word from array
int word_length = word.length();//finds length of word
guess = initialise(word_length);//creates dashes to display depending on length of word
wrongGuess = 0;//user wrong guesses
boolean[] history = new boolean[26];//stores users guesses in array so not repeated again
System.out.println(guess);//outsputs the current users guess
//while loop to repeat the users guess until lives run out
while(wrongGuess <10 && !guess.equals(word))
{
userGuess = getGuess(guess, wrongGuess);//gets the users next guess
if (userGuess < 'a' || userGuess > 'z')//ensures that guess is a letter
invalidLbl.setText(invalidLbl.getText() +("Invalid guess " + userGuess));//display output if guess is not a letter and displays what they entered
else if (history[userGuess - 'a'])//checks history array to ensure guess has already not be used
guessedLbl.setText(guessedLbl.getText() +("You already guessed " + userGuess));//display output if guess has been entered before
else
{
history[userGuess - 'a'] = true;//enters letter as guessed to histroy array
if(inWord(userGuess, word)) //checks if guess letter is in word and enters in to display
guess = fillInGuess(userGuess, guess, word);//adds the guessed letter to variable
else//wrong guess
wrongGuess++;//chnages amount of wrong guesses (adds on, loses a life)
}
//if statement which displays answer is number of wrong guesses is reached
if(wrongGuess==10)
JOptionPane.showMessageDialog(null, "The answer was: " + word);//output to user is maximum guesses has been reached
}again=goAgain();// opens goAgain method
} while (again=='Y');//repeats if answer to restart game is "Y"
}//close playgame method
//start goAgain method
public char goAgain()
{
//display output to prompt user to go again, if yes do loop is repeated above
return JOptionPane.showInputDialog("Do you want to play again? (Y/N)").toUpperCase().charAt(0);
}
//select random word method
public String selectRandomWord(String[] words, int num)
{
//selects a random word from array
return words[generator.nextInt(num)];
}
//start initialise method
public String initialise(int word_length)
{
//sets the number of "-" to be displayed depending on length of word
String temp="";
for(int i=0;i<word_length;i++)
temp+="-";
return temp;
}
//start getGuess method
public char getGuess(String currentGuess, int numWrong)
{
//output on optionpane to prompt user to enter another guess and shows how many wrong guesses made
return JOptionPane.showInputDialog("Your current guess is " +
currentGuess + "\n\n"+ numWrong + "/10" + " wrong guesses so far\n"
+ "\nEnter your next guess").toLowerCase().charAt(0);
}
//start inWord method
public boolean inWord(char userGuess, String word)
{
//checks if users guess is a charactor in the word
boolean returnvalue=false;//if not returns as false
for(int i=0;i<word.length();i++)
if(userGuess==word.charAt(i))
returnvalue=true;//is yes returns as true
return returnvalue;
}
//start method fillInGuess
public String fillInGuess(char userGuess, String guess, String word)
{
//fills in the correctly guessed charactor to the position in the word to display in panel
String temp="";
for(int i=0;i<word.length();i++)
{
if(guess.charAt(i)!='-')
temp=temp+guess.charAt(i);
else if(word.charAt(i)!=userGuess)
temp=temp+"-";
else temp=temp+userGuess;
}
return temp;
}//ends fillInGuess method
}
//end class
}
Where do you add the action listener to any buttons?
Re: JOption to JFrame HELP...messed my program up
oh sorry i thought i attached it wrong.
can you help with my code as you can see some of the joption outputs are still in there but i created a new class and tried my best at creating it in jframes but i have a hit a wall
Re: JOption to JFrame HELP...messed my program up
Using a GUI is sort of the opposite of using prompts with JOptoinPanes.
You present the GUI to the user, he fills in data or checks some options and presses a button.
The program then collects the data and does its processing, displays some results and returns to the java program to wait for more user input. You want to keep the processing in the listeners as short as possible.
Using JOptionPane for error messages can be ok.
How is the program supposed to work? When I start it all I get is a grey window and a button.
Re: JOption to JFrame HELP...messed my program up
it is supposed to start with a screen saying current guess which would be " - - - - -" then when the users guesses a letter the sapce should be updated if guessed correctly if not there should be an output also saying the number of guesses made (could be known as lives) once reaching 10 the game should automatcally stop and tell the user the word and ask if he wants to play again.
i have completly hit a wall because when we were getting lectures on jframes i was hard at the program putting option panes in...
Re: JOption to JFrame HELP...messed my program up
Quote:
Originally Posted by
Norm
Using a GUI is sort of the opposite of using prompts with JOptoinPanes.
You present the GUI to the user, he fills in data or checks some options and presses a button.
The program then collects the data and does its processing, displays some results and returns to the java program to wait for more user input. You want to keep the processing in the listeners as short as possible.
basically all the code i have put in the actionlistener for the start button is ALL the code i have in my orginal program using joption
Re: JOption to JFrame HELP...messed my program up
You are going to have to redesign the logic to use fields in the GUI instead of the option pane prompts.
There won't be a loop inside of your code. The "loop" will be sort of like this:
you present the user with some data and fields, he enters data and presses a button, you do something with the data, show some results in some fields in the GUI and then exit back to the JVM. Then the JVM waits for the user to respond. When the user has entered data, and presses a button the execution goes around again as above.
Re: JOption to JFrame HELP...messed my program up
Quote:
basically all the code i have put in the actionlistener for the start button is ALL the code i have in my orginal program using joption
The loop will have to come out of the listener. You can not loop inside of the listener. The looping will be as I described in the last post.