Results 1 to 10 of 10
  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default two "extend" calls

    I am working with a program where I am required to use a JFrame in a child class. The only way that I know how to access a JFrame is to do, example (public class Example extends JFrame), but since it is already extending the parent class, I am kind of stuck. I do not think that you can extend two separate classes, so..... I am stuck.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: two "extend" calls

    Can the child class use a new statement to create an instance of a JFrame?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: two "extend" calls

    I am not exactly sure what you are alluding to. Do you mean use two seperate statements that use the call extends?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: two "extend" calls

    No. What he means is simply instantiating an instance of JFrame in your class and then add your class to it. You don't need to extend JFrame (nor should you).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: two "extend" calls

    Does this mean, in my main method, to have something like example frame = new example where "example" in the name of an object (i.e. public example() ) in my child class??

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: two "extend" calls

    Yes, except "example" would be the name of the class. And by convention, class names start with an upper case letter.

    So here is a possible example:

    Java Code:
    public class YourClass extends JPanel {
        JFrame frame;
        public YourClass() {
            frame = new JFrame();
            frame.add(this); // adding instance of YourClass to JFrame
            // other stuff in constructor.
        } 
        // rest of class
    }
    This is just an example. You will have to determine what is appropriate for your specific assignment.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: two "extend" calls

    Perhaps it is easiest if I show you what I have done and indicate the error:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class CreateAliens
    {
      public static void main (String[] args)
      {
        Martian aMartian = new Martian();
        Jupiterian aJupiterian = new Jupiterian();
        
        JOptionPane.showMessageDialog
          (null, "\nMartian and Jupiterian Descriptions: \n" +
           aMartian.toString() + "\n" + aJupiterian.toString());
        
        MartianGraphics frame = new MartianGraphics();
        frame.setSize(300,200);
        frame.setVisible(true);
      }
    }
    Java Code:
    public abstract class Alien
    {
      private int numEyes;
      protected int numLegs;
      protected int numArms;
      
      public Alien(int numEyes, int numLegs, int numArms)
      {
        setNumEyes(numEyes);
        setNumLegs(numLegs);
        setNumArms(numArms);
      }
    
      public int getNumEyes()
      {
        return numEyes;
      }
      
      public void setNumEyes(int eyes)
      {
        numEyes = eyes;
      }
      
      public int getNumLegs()
      {
        return numLegs;
      }
        
      public void setNumLegs(int legs)
      {
        numLegs = legs;
      }
      
      public int getNumArms()
      {
        return numArms;
      }
      
      public void setNumArms(int arms)
      {
        numArms = arms;
      }
      
      public String toString()
      {
        return ("The alien has " + getNumEyes() + " eyes, " +
                getNumLegs() + " legs, and " + getNumArms() + " arms.");
      }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Martian extends Alien
    {
      public int xStart;
      public int yStart;
      
      public Martian()
      {
        super(2, 3, 1); //sets 2 eyes, 3 legs, and 1 arm
      }
      
      public String toString()
      {
        return("The Martian has " + getNumEyes() + " eyes, " +
                getNumLegs() + " legs, and " + getNumArms() + " arms.");
      }
      
      [COLOR="#FF0000"]public MartianGraphics()[/COLOR]
      {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      
      public void DrawMartian()
      {
        String nameMartian = new String("I'm Marvin the Martian");
      }
      
      public void paint (Graphics gr)
      {
        super.paint(gr);
        gr.drawString(nameMartian, 30, 100);
        pen.drawOval(50, 170, 70, 70);
        pen.drawLine(85, 240, 90, 280);
        pen.drawLine(90, 280, 60, 380);
        pen.drawLine(90, 280, 110, 380);
      }
    }
    It is the code that is in red that seems to be the error.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: two "extend" calls

    Methods should have a return type (e.g. 'void').

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: two "extend" calls

    I changed public DrawMartian to public void DrawMartian and it throws 11 errors. Is there anything else that I might be missing??

  10. #10
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: two "extend" calls

    What is the first error it throws as that usually is the import one.
    With the right know how, anything is possible

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  4. JSP sending a String to a "mini App" and calls that program too.
    By pikapika2501 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2009, 12:19 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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