开发者

error in running a simple swing program

i just started learning swings. And thought 开发者_如何学Cof 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.


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

     }
}


As vodkhang said, you need a "main" method. Make sure it looks just like this:

public static void main(String[] args)
{
  // your code here.  
  // this example will use your panel:

  // create a new MyDrawPanel 
  MyDrawPanel panel = new MyDrawPanel();

  // create a frame to put it in
  JFrame f = new JFrame("Test Frame");
  f.getContentPane().add(panel);

  // make sure closing the frame ends this application
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // show the frame
  f.setSize(100,100);
  f.setVisible(true);

}

Yes, every Java program that you want to run needs a main method with exactly this signature:

  public static void main(String[] args)

You can run java code from within other systems (like web servers and etc without a "main") but to simply run it, the main is the entry point. Put it where-ever you want to start a program running.

When running, make sure you get the class name right to help it find your main method. In your case, if you are running java by hand in the same directory as your MyDrawPanel.class file you would do this:

  java -cp . MyDrawPanel

If you are running from inside a developer tool, then it will provide a way to run the class you are looking at.


Do we need to have a main method in every class we want to run?

You need a class with a main method to start a JVM.

Can't JVM run any class which doesnt have a main method.

Not initially.

Here i don't require a main class i think, cuz this paintComponent method should be called by the system, right?

Wrong. It's true that the paintComponent() method will eventually be called by "the system", specifically the Swing Event Dispatch Thread. But that needs to be started first, which happens implicitly when you create a window and make it visible. And that in turn can only happen in a main method.


You need a main method in the class that you want to run the program for. It is mandatory. How can the JVM know which method they should call to start if you have multiple methods. They can guess but most of the time, the guess may go wrong. So, provide a simple main method will help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜