Results 1 to 17 of 17
  1. #1
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default 2 Errors Please help em figure out what is wrong with this

    Here is my code, Only 1 error now!
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class SaucerPanel extends JPanel{
    static final long serialVersionUID = 1;
    public SaucerPanel(){
    setPreferredSize(new Dimension(500,400));
    setBackground(Color.red);
     }
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawLine(0,350,500,350);
    g.drawRect(200,180,30,170);
    g.drawRect(350,240,15,110);
    g.setColor(Color.black);
    g.fillOval(10,180,100,30);
    g.drawString("hovering...",10,230);
    }
    }
    public class SaucerPanel extends JPanel implements ActionListener {
      JButton Up = new JButton("Up"); 
      JButton Down = new JButton("Down");
      JButton Left = new JButton("Left");
      JButton Right = new JButton("Right");
      JButton quit = new JButton("Quit");
      
      public SaucerPanel(){
        setBackground(Color.red);
        this.add(Up); 
        Up.addActionListener(this);
        this.add(Down); 
        Down.addActionListener(this);
        this.add(Left); 
        Left.addActionListener(this);
        this.add(Right); 
        Right.addActionListener(this);
        this.add(quit); 
        Quit.addActionListener(this);
        
    public void actionPerformed(ActionEvent e){ 
        if (e.getSource() == Quit)
          System.exit(0);
        else if(e.getSource() == Up){
          g.setColor(Color.blue); 
           xpos = x
           ypos = y+5;
           g.drawString("hovering...",xpos,ypos+5);
           repaint();
        }
         else if(e.getSource() == Right){ 
           g.setColor(Color.yellow);
           xpos = x+5;
           ypos = ypos;
           g.drawString("hovering...",xpos+5,ypos);
           repaint();
         }
         else if(e.getSource() == Left){ 
           g.setColor(Color.green);
           xpos = x-5;
           ypos = ypos;
           g.drawString("hovering...",xpos-5,ypos);
           repaint();
         }
           else if(e.getSource() == Down){ 
           g.setColor(Color.Black);
           xpos = x;
           ypos = ypos-5;
           g.drawString("hovering...",xpos-5,ypos);
           repaint();
         }
    }
    Here is the error
    File: C:\Documents and Settings\jbirse\SaucerPanel.java [line: 20]
    Error: The type SaucerPanel is already defined

    (NOTE this refers to the line "public class SaucerPanel extends JPanel implements ActionListener {")

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    yeah but when i change it to something else like SaucerButtons I get a different error message

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I get a different error message
    It'd help if you'd post the error message and show the code that causes it.

    I would guess the error is having two public class statements in the same source. Remove the "public" from one of them or put them in separate source files with appropriate names.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jwb4291 View Post
    yeah but when i change it to something else like SaucerButtons I get a different error message
    I can see in my crystal ball that there are other errors in your code, I just can't see what yet ...

    kind regards,

    Jos

  6. #6
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    This is a homework program and it says to Submit the code for "SaucerPanel.java" So everything has to go under Saucerpanel.java, i can't split it into two different files because everything needs to be submitted at once.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jwb4291 View Post
    This is a homework program and it says to Submit the code for "SaucerPanel.java" So everything has to go under Saucerpanel.java, i can't split it into two different files because everything needs to be submitted at once.
    Please elaborate as I don't understand what you mean here.

    In case you didn't understand my first post, you have declared two classes with the same name, SaucerPanel", and apparently in the same file, both public no less:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public [b][color="red"]class SaucerPanel[/color][/b][color="red"][/color] extends JPanel{
      static final long serialVersionUID = 1;
      public SaucerPanel(){
         setPreferredSize(new Dimension(500,400));
         //... code deleted for sake of brevity
      }
    }
    
    public [b][color="red"]class SaucerPanel[/color][/b][color="red"][/color] extends JPanel implements ActionListener {
      JButton Up = new JButton("Up"); 
      // .... code deleted for sake of brevity
    }
    You can't even have two public stand alone classes in the same file, much less two classes of the same name.
    Last edited by Fubarable; 08-09-2010 at 08:17 PM.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jwb4291 View Post
    This is a homework program and it says to Submit the code for "SaucerPanel.java" So everything has to go under Saucerpanel.java, i can't split it into two different files because everything needs to be submitted at once.
    A Java source file can contain one public class or interface at most; other classes or interfaces in that file can only have package scope and (of course) all must have a unique name.

    kind regards,

    Jos

  9. #9
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please elaborate as I don't understand what you mean here.

    In case you didn't understand my first post, you have declared two classes with the same name, SaucerPanel", and apparently in the same file, both public no less:

    You can't even have two public stand alone classes in the same file, much less two classes of the same name.
    Ok, i can see what you are saying and I can see how it is wrong but I don't know how to change it or what to change it to.

    and an elaboration of my homework post: The assignment is create a saucer(oval) the rises above a building, goes right, then goes down in between two buildings using buttons.
    The professor gave a prompt and at the end it says
    Enter your SaucerPanel.java class here:

    So I'm having difficulty figuring out how to get everything into one "SaucePanel.java class"

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Only have one "public" class in the SaucePanel.java named SaucePanel
    You can has as many non-public classes as you want in a .java file.
    The output from the javac compiler will be one .class file for every class defined in the source file.

  11. #11
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    So what do you really want? One panel with name "SaucerPanel" or two panels (and they must be with two different names :D)?

  12. #12
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by cselic View Post
    So what do you really want? One panel with name "SaucerPanel" or two panels (and they must be with two different names :D)?
    what id like is just one continuous code! haha so Norm would I just remove Public from the second SaucerPanel? and I tried that and I wound up with 43 errors.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Each class must have a unique name.
    How can you have more than one class with the same name? How would the compiler know which one you wanted to use?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    Default

    I predict a thread with more than fifty replies or so and no positive ending.

    kind regards,

    Jos

  16. #16
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    what id like is just one continuous code!
    Then you must delete one "public SaucerPanel()" or "public class SaucerPanel extends JPanel implements ActionListener".

    I vote for deleting first one.
    Then you should join code from deleted "public SaucerPanel()" to "public class SaucerPanel extends JPanel implements ActionListener". Use copy-paste to do that :D
    Also what you have in "public SaucerPanel()" it must be copied to "public SaucerPanel()" in "public class SaucerPanel extends JPanel implements ActionListener" and so on :cool:

  17. #17
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I predict a thread with more than fifty replies or so and no positive ending.

    kind regards,

    Jos
    false, got it working within 30 minutes

Similar Threads

  1. Cant figure out where went wrong.
    By leviathan in forum New To Java
    Replies: 15
    Last Post: 06-06-2010, 06:55 PM
  2. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  3. Whats Wrong? Errors when compiling
    By software_dude_2009 in forum New To Java
    Replies: 5
    Last Post: 05-12-2009, 12:19 PM
  4. [SOLVED] Can't figure out what's wrong
    By roach_van in forum New To Java
    Replies: 10
    Last Post: 09-05-2008, 07:09 AM
  5. Replies: 2
    Last Post: 07-11-2008, 09:41 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
  •