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

Thread: Error in calling JFrame's super.

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

    Question Error in calling JFrame's super.

    Hi, I'm new here, and I'm trying to learn how to properly use JFrame and some other graphical coding using Java. My problem right now is that my book suggested a code that keep giving me an error when compiling. The error I get from the code below says "call to super must be first statement in constructor.", yet I don't see what I did wrong. Would you all be kind enough to help me solve this please?

    import javax.swing.*;
    import java.awt.*;
    public class ProjectOneJFrame extends JFrame
    {
        public void JFrameDemo()
        {
            super("JFrameDemo");
            setSize(400,400);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public void paint(Graphics g)
        {
            super.paint(g);
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 400, 400);
            g.setColor(Color.orange);
            g.setFont(new Font("Arial", Font.BOLD, 18));
            g.drawString("Doing graphics with a JFrame!", 60, 200);
        }
        public static void main (String[] args)
        {
            new JFrameDemo();
        }
    }

    Also, I would appriciate it if anyone notes any other errors in my coding.(Just so you know, I use BlueJ and NetBeans to code my programs.)


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Error in calling JFrame's super.

    The message "call to super must be first statement in constructor" means what it says: you can only call super() as the first line of a constructor. But you have "super("JFrameDemo");" as the first line of a method. I know it's a method (and so does the compiler) because it returns void, and has a name that is not the same as that of the class.

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

    Default Re: Error in calling JFrame's super.

    -_- Thank you very much. I feel stupid now, lol. I fixed my coding so that it was the same name as the class, and changed the line in the main method and it worked. Thank you for helping me solve that, I don't think I'll forget about this now.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Error in calling JFrame's super.

    You're welcome.

Similar Threads

  1. Replies: 1
    Last Post: November 21st, 2011, 06:22 AM
  2. Replies: 2
    Last Post: November 9th, 2011, 12:12 AM
  3. Super Keyword
    By shruthi in forum Java Theory & Questions
    Replies: 12
    Last Post: October 13th, 2011, 03:31 PM
  4. Junit3 error: Implicit super constructor TestCase() is not visible
    By albertkao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 12:53 PM
  5. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 05:35 AM

Tags for this Thread