Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 979143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:10:25+00:00 2026-05-16T04:10:25+00:00

i just started learning swings. And thought of trying out a simple program, but

  • 0

i just started learning swings. And thought of trying out a simple program, but i can’t run it.

import java.awt.*;
import javax.swing.*;
class MyDrawPanel extends JPanel 
{
    public void paintComponent(Graphics g) 
    {
        g.setColor(Color.orange);
        g.fillRect(20,50,100,100);
    }
}

I am getting the following error:

Exception in thread "main" java.lang.NoSuchMethodError: main

My Question: Do we need to have a main method in every class we want to run? Can’t JVM run any class which doesnt have a main method. Here i don’t require a main class i think, cuz this paintComponent method should be called by the system, right?

P.S: I am using plain vanilla cmd to compile and run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:10:26+00:00Added an answer on May 16, 2026 at 4:10 am

    java is rather simple, when you give it a class file it will load it and try execute a program. Java programs are defined to start in the “public static void main(String… args)” method. So a class file missing this function has no valid entry point for a program.

    To make java call your paintComponent() method you have to add an instance of your class to a toplevel container like JFrame or for web applications an JApplet. (Applets don’t use a main method as they are executed as part of a web page and not a standalone app.)

    Example:

    import javax.swing.*
    public class MyDrawPanel{
         public static void main(String... args)
         {
             JFrame frame = new JFrame(200,200);//A window with 200x200 pixel
             MyDrawPanel mdp = new MyDrawPanel();//Panel instance
             frame.add(mdp);//Add the panel to the window
             frame.setVisible(true);//Display all
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit when the window is closed
    
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning Lisp and I can't figure out how to compile and
I have just started learning Erlang and am trying out some Project Euler problems
Just started learning Rails (3). I am tearing my hair out trying to find
I just started learning code (Java specifically), and i'm testing out a password system
I just started learning JavaScript and am wondering why this simple snippet hangs when
Just started learning c++ today and im pretty boggled. its an amazing language but
I have just started learning C#. Can anyone explain the technical differences between a
I just started learning opengl and writing a first person shooter but I'm getting
Just started learning NServiceBus and trying to understand the concept. When it talks about
Hi Just started learning c for uni (usually use objective c) and have run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.