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 6 of 6

Thread: Error: Invalid method declaration; return type required. Related to JFrames.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Error: Invalid method declaration; return type required. Related to JFrames.

    Hello -

    I am currently practicing on developing a small game where you have to guess a number from 1 to 10, with three attempts allowed. The game will tell you if the secret number is lower or greater than your guess. I have already done it with user's keyboard input; now I want to do it using only the mouse.

    My problem is that I'm missing something, and I believe it's somethng simple, but I still can't find it. Here's the code:

    /*
     * Filename: GTNB.java
     * Created:  10-24-2012 by MH
     *
     * Purpose:  Conversion of Guess The Number game to button input.
    */
     
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
    public class GTNB extends JFrame
    {
     
       // set frame size
       private static final int WIDTH = 500;
       private static final int HEIGHT = 175;
     
       // declare component objects
       private JLabel labelB;
       private JButton j1B, j2B, j3B, j4B, j5B, j6B, j7B, j8B, j9B, j10B, jYB, jNB;
     
       // declare reference variables for event handlers
       private button1Handler  j1BHandler;
       private button2Handler  j2BHandler;
       private button3Handler  j3BHandler;
       private button4Handler  j4BHandler;
       private button5Handler  j5BHandler;
       private button6Handler  j6BHandler;
       private button7Handler  j7BHandler;
       private button8Handler  j8BHandler;
       private button9Handler  j9BHandler;
       private button10Handler j10BHandler;
       private buttonYHandler  jYBHandler;
       private buttonNHandler  jNBHandler;
     
       //frame constructor
       public GuessTheNumberButton()
       {
     
          // frame properties
          JFrame frame = new JFrame();
          setTitle("Guess The Number");
          setSize( WIDTH, HEIGHT );
          Container pane = getContentPane();  // JFrame method
     
          // create panel A
          JPanel panelA = new JPanel();
          // create panel B
          JPanel panelB = new JPanel();
     
          // instantiate panel A buttons
          JButton j1B = new JButton("1");
          JButton j2B = new JButton("2");
          JButton j3B = new JButton("3");
          JButton j4B = new JButton("4");
          JButton j5B = new JButton("5");
          JButton j6B = new JButton("6");
          JButton j7B = new JButton("7");
          JButton j8B = new JButton("8");
          JButton j9B = new JButton("9");
          JButton j10B = new JButton("10");
     
          // instantiate panel B label
          JLabel labelB = new JLabel(" ");
     
          // instantiate panel B buttons
          JButton jYB = new JButton("Yes");
          JButton jNB = new JButton("No");
     
          // add panel A buttons
          panelA.add(j1B); 
          panelA.add(j2B);
          panelA.add(j3B);
          panelA.add(j4B);
          panelA.add(j5B);
          panelA.add(j6B);
          panelA.add(j7B);
          panelA.add(j8B); 
          panelA.add(j9B);
          panelA.add(j10B);
     
          // add panel B label
          panelB.add(labelB);
          // yes/no buttons to be added later
     
          return 0;
       }  // end method
     
       public static void main (Strings[] args)
       {
          JFrame aGuessTheNumberButton = new GuessTheNumberButton(); //GuessTheNumberButton();
          aGuessTheNumberButton.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          aGuessTheNumberButton.getContentPane().add(BorderLayout.NORTH,panelA);
          aGuessTheNumberButton.setVisible(true);
       } // end main
     
    } // end class

    I haven't written the event handling yet; I'm just creating the frame at this moment. My problem is in line 38, the declaration of the method, where I get "Invalid method declaration; return type required". It can't be void, so what would it be then? Can someone provide a hint?

    Thanks!
    Last edited by jps; October 26th, 2012 at 02:14 PM. Reason: Fixed code tag


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error: Invalid method declaration; return type required. Related to JFrames.

    public GuessTheNumberButton(){
       //...
       return 0;
    }
    See something wrong with this picture?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Error: Invalid method declaration; return type required. Related to JFrames.

    Is GuessTheNumberButton supposed to be a method, or the class name? If the former, you must specify what is to be returned (eg void, etc...). If the latter, the name must match the definition of the class it contains (eg here you have it named GTNB)

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error: Invalid method declaration; return type required. Related to JFrames.

    Thanks to both for the replies!

    jps - sorry, the return does not apply, and I forgot to erase it. As copeg implies, I am confusing method with class. I will get back to you shortly with results.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error: Invalid method declaration; return type required. Related to JFrames.

    OK, I've gone back and cleaned up the code a bit, as it's been messy. Below is the code without references to event listeners or action handlers:

    /*
     * Filename: GTNB.java
     * Created:  10-24-2012 by MH
     *
     * Purpose:  Conversion of Guess The Number game to button input.
    */
     
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class GTNB extends JFrame
    {
     
       //frame constructor
       public GTNB()
       {
       // create top panel, to go on top border
       JPanel topPanel = new JPanel();
     
       // instantiate panel A buttons - components
       JButton j1B = new JButton("1");
       JButton j2B = new JButton("2");
       JButton j3B = new JButton("3");
       JButton j4B = new JButton("4");
       JButton j5B = new JButton("5");
       JButton j6B = new JButton("6");
       JButton j7B = new JButton("7");
       JButton j8B = new JButton("8");
       JButton j9B = new JButton("9");
       JButton j10B = new JButton("10");
     
       // add panel A buttons
       topPanel.add(j1B); 
       topPanel.add(j2B);
       topPanel.add(j3B);
       topPanel.add(j4B);
       topPanel.add(j5B);
       topPanel.add(j6B);
       topPanel.add(j7B);
       topPanel.add(j8B); 
       topPanel.add(j9B);
       topPanel.add(j10B);
     
       // create frame in which panels will be placed
       JFrame frameBig = new JFrame();
       frameBig.add(topPanel); //, BorderLayout.NORTH);
       }
     
       public static void main (String[] args)
       {
          JFrame window = new GTNB();
          window.setSize(500,175);
          window.setTitle("Test");
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.getContentPane().add(BorderLayout.NORTH,topPanel);
          window.setVisible(true);
       } // end main
     
    } // end class

    The error is now the following: on line 57, error: cannot find symbol. The symbol is topPanel. I want the window to use the Border layout, so in NORTH I can have the 10 buttons and on SOUTH I can have labels.

    Do I have to declare topPanel somewhere? If so, where and how? And why (I'm making reference to it in main, but should have declared it elsewhere)?

    Thanks!
    Last edited by jps; October 26th, 2012 at 05:37 PM. Reason: [code=java]

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error: Invalid method declaration; return type required. Related to JFrames.

    window.getContentPane().add(BorderLayout.NORTH,top Panel);
    perhaps
    window.getContentPane().add(topPanel, BorderLayout.NORTH);
    I think the component is the first parameter in add...

    ...and:
    topPanel is a variable local to the constructor, and not visible to the main method.

Similar Threads

  1. Replies: 4
    Last Post: February 26th, 2012, 05:36 PM
  2. Replies: 3
    Last Post: October 28th, 2011, 04:42 PM
  3. error: invalid meyhod declaration
    By iswan in forum AWT / Java Swing
    Replies: 0
    Last Post: September 30th, 2011, 08:03 PM
  4. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 01:56 AM
  5. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM