Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Guess Number (Frame & btnHandler)!!!! HELP

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guess Number (Frame & btnHandler)!!!! HELP

    I can get GUI run as i want to... Can any1 help me to look at it!

    Write a application that plays “guess the number” as follows: Your application chooses the number to be guessed by selecting an integer at random in the range 1-100. The application then displays the following in a label:

    I have a number between 1 and 100. Can you guess my number?

    A JTextField should be used to input the guess. As each guess is input, a hint message will be output to tell the user if the number enters is too low or too high.

    A JButton should be provided to allow the user to play the game again. When the JButton is clicked, a new random number should be generated.

    (Size of the window is width: 280 pixels and height: 150 pixels)

    import java.util.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JOptionPane;
     
    public class Q3 {
     
         JTextField number;
         JLabel lbl2;
     
         double num1=0.0;
     
            Q3(){
                JFrame frm =  new JFrame("Guessing Game");
                frm.setSize(280,150);
                frm.setTitle("Guessing Fame");
                frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
                JLabel lbl = new JLabel("I have number between 1 and 100");
                lbl2 = new JLabel("Can you guess my number?");
                number = new JTextField(15);
     
                JButton btn = new JButton("New Game");
                JPanel panel = new JPanel();
                number.addActionListener(new ButtonHandler());
                panel.add(lbl);
                panel.add(lbl2);
                panel.add(number);
     
                panel.add(btn);
                frm.add(panel);
                frm.setVisible(true);
     
            }
     
        public static void main(String[] args) {
            Q3 game = new Q3();
            Random generator = new Random();
     
                int n1=generator.nextInt(101);
                System.out.println(n1);
     
     
        }
     
        private class ButtonHandler implements ActionListener{
     
            public void actionPerformed(ActionEvent e) {
                int num1;
     
                int guess = 0;
                String num=number.getText();
     
                    try{
                        num1 = Integer.parseInt(num);
                        do{
                //num1 = Integer.parseInt(num);
     
                /*if(num1==guess){
                lbl2.setText("Congratulations,You guess is correct!");
                }
                else*/ if(num1<=guess)
                    lbl2.setText("Too low, try a higher number");
     
                else if(num1>=guess)
                    lbl2.setText("Too high, try a lower number");
     
                else
                    lbl2.setText("Congratulations,You guess is correct!");
                        }while(guess!=num1);
                           number.setText("");
     
            }
            catch(Exception ex){
                JOptionPane.showMessageDialog(null,"Please Enter the correct input");
                number.setText("");
            }
     
            }
            }
     
        }

    Pls any1 can help me with this???
    TQ!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Guess Number (Frame & btnHandler)!!!! HELP

    First, shouting in large font will often times persuade anyone wanting to help to spend their time elsewhere (as do the help and exclamation points in your title). Second, use the code tags. Lastly, what is the question? Dumping a homework assignment and a bunch of code with no defined question will more than likely not get you much help (but you never know...). Suggested reading: How To Ask Questions The Smart Way

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Guess Number (Frame & btnHandler)!!!! HELP

    The problem seems to be that you're adding your ButtonHandler to a JTextField.

Similar Threads

  1. how to set my frame in the middle of the screen
    By chronoz13 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 10th, 2012, 08:13 AM
  2. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 05:35 AM
  3. [SOLVED] frame are exiting at the same time
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: January 25th, 2010, 08:13 PM
  4. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 15th, 2009, 12:58 AM
  5. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 11:36 AM