Looking for a little insight as to why this does not work
Hi guys I am having a problem with my Java code. I was hoping maybe someone here could help me out with it. I am totally new to programming with java so don't be too harsh.
This is supposed to be a guessing game where the user guesses the number. Here are the specifics:
Your program chooses the number to be guessed by selecting an integer at random in the range 11000. The program then displays the following in a label.
I have a number between 1 and 1000 -- can you guess my number?
Please enter your guess: ____________________
A JTextField should be used to input the guess. A JButton must be provided to allow the user to press each time a guess is entered. As each guess is input, the background color should change to either red or blue. Red indicates that the user is getting warmer, and blue indicates that the user is getting colder. A JLabel should display either Too High or Too Low to help the user zero in on the correct answer. When the user gets the correct answer, Correct! should be displayed, and the JTextField used for input should be cleared and changed to be uneditable. Also, a JButton should be provided to allow the user to play the game again. When the New Game JButton is clicked, a new random number should be generated and the input JTextField changed to be editable. A JButton must be provided to allow the user to exit the application. Also provide a count of the number of guesses the user entered when the correct number is guessed.
This is the code I have put together:
I really appreciate any help I can get on this.
You don't do anything to generate a random number to start with. The only time you create one is when you successfully guess the random number. So you need to generate that elsewhere during construction.
The colour changing is working properly, just not the way you want it. You are attempting to set the background of the JFrame, not the panel. Change those to panel.setBackground and it will work fine.
You should do some more evaluation of the structure here. GuessGame is a JFrame, but doesn't make any use of being as such. Instead it invokes its own JFrame and shows that, so this means you cannot call any component specific calls on the nested classes without providing an object to work with since the JFrame that would resolve to the implicit "this" of the parent wouldn't have any impact on the existing gui. Also, these properties are all static. If you leave them as such, then it will impact on the new game functionality which spawns a new frame. Both of these then share the same random number and all the components will be the same. It'll likely break the event handlers too I'd expect since the focusing will become. . . bizarre.
Move the control of the guessing to the GuessGame. Let the trybuttonhandler simply call the GuessGame methods.
Ok great thank you so much. I have no idea what some of this means but at least i have something to go on. Instead of wandering aimlessly trying to figure out what is wrong, I can wander aimlessly and figure out how to fix what is wrong (LOL - JK - This is a homework assignment so what you gave is exactly what I needed). Thank you so much for your help. :thumbsup:
About your answer
I just have one question about your reply though, if you don't mind.
What is meant by this:
" GuessGame is a JFrame, but doesn't make any use of being as such. Instead it invokes its own JFrame and shows that, so this means you cannot call any component specific calls on the nested classes without providing an object to work with since the JFrame that would resolve to the implicit "this" of the parent wouldn't have any impact on the existing gui."
This part left me a little lost.
GuessGame is already a JFrame; add all your components directly to it instead of creating mainFrame within it.
Thanks I got it now. It works great. It may not be completely structured as it should be, but it is working. I am hoping pretty soon all of this is just going to come together.
|All times are GMT +1. The time now is 03:07 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.