Needs help in logic

E

Eric

Hi guys: Can any one please help me in that program. I try it but
stuck in it.

ask user to enter random number between 1 to 1000 using JTextfield.
Input the user's guess in the code for a previously-registered event-
handler method or the actionPerformed method of class based on the
ActionListener
interface. For the first guess color the entire background red. If
this is the second or later guess, and they are further from the
correct number than the last guess, then color the entire background
blue. If they get the correct number then
color the background some other color than red or blue. If the user
guessed the number correctly, respond with their number, post a
congratulatory message, get a new random number, and display a JButton
to start a new game. Otherwise, to help the user close in on the
correct number, post a message, with their guessed number, whether
they are "TOO HIGH" or "TOO LOW" from the correct number, and whether
they are "WARMER" or "COLDER" (this should match the background
color). Also report the guess number of the next guess (e.g. "Enter
guess number nnn"). use a concatenated string in JLabel for these
incorrect guess messages. The process is repeated each game until the
user guesses the correct number. erase obsolete status messages.



import java.awt.*;
import javax.swing.*;
public class guessprogram {
public static void main(String args[]){
String title = (args.length == 0 ? "Guess Program"
: args[0]);
JFrame frame = new JFrame(title);
Container content = frame.getContentPane();
JPanel namePanel = new JPanel(new BorderLayout());
JLabel nameLabel = new JLabel("Enter Number: ");
JTextField nameTextField = new JTextField();
nameLabel.setLabelFor(nameTextField);
namePanel.add(nameLabel, BorderLayout.WEST);
namePanel.add(nameTextField, BorderLayout.CENTER);
content.add(namePanel, BorderLayout.NORTH);
frame.setSize(250, 150);
frame.setVisible(true);
}
}
 
M

markspace

Well, I don't do homework for people, but I'll give you some hints.

1. Don't use BorderLayout, it kinda sucks for what you're trying to do.
The default FlowLayout of a JPanel is proabably better. Put all the
components in the JPanel and add that to the main content pane of the
JFrame. Then pack() the frame (frame.pack()) instead of setting its size.

2. This is the next bit to add to your program:
event-
handler method or the actionPerformed method of class based on the
ActionListener

Read this:

<http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html>

Then add a method to your main class like this:

public class guessprogram implements .... name here... {

public static void main(String args[]) {
// this is what you have already...
}

public void actionPerformed(ActionEvent e) {
// logic to test input here
}

}

And add the ActionListener as that link shows you. That should get you
started. Your program has threading problems, but we'll leave that
alone for a beginner problem.
 
L

lewbloch

markspace said:
... Your program has threading problems, but we'll leave that
alone for a beginner problem.

But not for very long, just long enough to get you through the
immediate questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top