Why do I get NoSuchMethodError: main in my Applet?
I need help in my Java applet.
import java.awt.*;
import java.applet.*;
public class Screen extends Applet{
public void init(){
setSize(300,300);
setBackground(Color.BLACK);
}
}
This error keeps poppi开发者_如何学编程ng up when I run it.
Exception in thread "main" java.lang.NoSuchMethodError: main
You are trying to run the applet as an application using
java Screen
That won't work, because an applet is not an application, and doesn't normally have a main()
method, which is what java Screen
will try to run.
There are a few solutions:
- Run it using the
appletviewer
tool, which comes with the JDK. - Run it in the browser by embedding it in an HTML page.
- Make it an "app-applet".
It's been almost ten years since I've actually written an applet so I don't remember the details around #3, but IIRC it involves adding a main()
method to the applet and having that main()
method launch the applet. I'm sure you can find it on Google.
Create a new swing form. Since your class extends the default 'applet' you can simply add it to a swing application like this Screen x = new Screen();
then add it to your JFrame YourJframe.add(x);
Hope this helped! (I could use an upvote lol :))
精彩评论