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

Thread: Code wont run. I know there is no main class, but this is from a book. Is it wrong??

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Code wont run. I know there is no main class, but this is from a book. Is it wrong??

    import java.awt.*;

    import javax.swing.*;
    import java.awt.color.*;

    public class ColorPanel extends JPanel{

    public ColorPanel(Color backColor)
    {
    setBackground(backColor);

    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.setColor(Color.blue);
    g.drawRect(10, 5, 120, 20);
    g.setColor(Color.red);
    g.drawString("Hello", 20, 20);


    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Code wont run. I know there is no main class, but this is from a book. Is it wro

    You need a main method, and you need to add that JPanel to something. I guess the book assumed you knew that part, as this is just a piece of a larger program. Does the book show you how to add JPanels to a JFrame, for example? And how to run a program that displays that JFrame? It's your job to put those pieces together.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Code wont run. I know there is no main class, but this is from a book. Is it wro

    u must add something like this:
     
    public class Main {
        public static void main(String[] args) {
            final JFrame fr = new JFrame();
            JPanel pan = new ColorPanel(Color.black);
            fr.add(pan);
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
                    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    fr.setSize(200, 200);
                    fr.setVisible(true);
                }
            });
        }
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Code wont run. I know there is no main class, but this is from a book. Is it wro

    Quote Originally Posted by remigio View Post
    u must add something like this:
    Recommended reading: The Problem with Spoon-feeding
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Code wont run. I know there is no main class, but this is from a book. Is it wro

    i understand
    do not paste code...
    btw. im new on this page..
    Last edited by remigio; July 20th, 2011 at 02:37 PM.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Code wont run. I know there is no main class, but this is from a book. Is it wro

    Also, I think paintComponent() is a protected method of JPanel.

    Yep. I checked to make sure and it is, though it's not a JPanel method directly. It's a JComponent method that JPanel inherited.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Code wont run. I know there is no main class, but this is from a book. Is it wro

    Quote Originally Posted by javapenguin View Post
    Also, I think paintComponent() is a protected method of JPanel.

    Yep. I checked to make sure and it is, though it's not a JPanel method directly. It's a JComponent method that JPanel inherited.
    Sorry, but what does that have to do with anything?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What is Wrong With My Exception Class Code?
    By Allicat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2011, 10:09 AM
  2. What is wrong with this code class not found
    By newbieJava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2011, 05:39 PM
  3. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 03:40 PM
  4. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 10:36 AM
  5. Is my book bad or am I doing 'wrong'?
    By Catgroove in forum Java Theory & Questions
    Replies: 6
    Last Post: November 16th, 2010, 01:51 PM