Results 1 to 6 of 6
  1. #1
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default How to show the "results" of a method in an applet

    Hello! I'm new to java and I could really use some help...:S I have made a simple game which we have a random secret number and we have 6 guesses to find it...One method of this class is :
    Java Code:
     public void guess(int x){
            while(x>0 && x<101 && noOfGuesses<8){
                if(secret==x)
                    System.out.println("We have a winner!!!");
                else if(secret >x){
                    System.out.println("False!Guess no"+noOfGuesses+" !" +" Guess again!The secret number is greater than "+x);
                    noOfGuesses++;}
                else {
                    System.out.println("False! Guess no"+noOfGuesses+" ! "+" Guess again!The secret number is smaller than "+x);
                    noOfGuesses++;}    
                break;
            }
            if(x<=0 || x>100)
                System.out.println("The input number is out of boundaries...The secret number is in the range [1,100]");
            if(noOfGuesses==7)
                System.out.println(" The secret number was " + secret+" Next time!");
        }
    Then, I'm trying to make a really simple applet.The applet contains only a text field, where the user writes the input number(his guess), and a JButton called "Enter".The problem is that even I'm calling the specific method ,the applet doesn't do anything.
    Java Code:
        public class ChancesListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                String operation=e.getActionCommand();
                if (operation.equals("Enter")){
                    String text=chancesDisplayField.getText();
                    int b=Integer.parseInt(text);
                    chance.guess(b);
                }
    Thanks in advance! :)

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to show the "results" of a method in an applet

    You need to define how you wish to display a result to the user. The method as posted prints to the command line...do you wish to show the result in the applet? If so, think about adapting your code to use a JLabel How to Use Labels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to show the "results" of a method in an applet

    Thank you very much for your quick respond! I'll try this!

  4. #4
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to show the "results" of a method in an applet

    Hello! Sorry to bother you again. So I added another JLabel, but then an error came up telling me that I had a problem because the method (guess) from the class is void. So, I changed the method to a String Method.Now, the class and the applet are both compiling but nothing comes up when I'm running the applet...I can't understand what is my error..
    Java Code:
      
     public class ChancesListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                String operation=e.getActionCommand();
                if (operation.equals("Enter")){
                    String text=chancesDisplayField.getText();
                    int b=Integer.parseInt(text);
                     Results.setText(chance.guess(b));
                }

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to show the "results" of a method in an applet

    Please provide a short, complete, and compileable example that domonstrates the problem. I can guess at the many ways you may see the behavior you see, but guesses can often be incorrect and even confusing.

  6. #6
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: How to show the "results" of a method in an applet

    Hello! Sorry for my belated answer! I found out the solution.I had forgotten to add another line in the ChancesListener class that should display the answer in the textfield that I had created..The thing about the string method was an error..I changed back to normal and everything work out! Thank you very much for your time and your help!

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. Formatting this output to show "#.##"
    By twiggy62 in forum New To Java
    Replies: 9
    Last Post: 02-06-2010, 05:40 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •