开发者

Java Program Error [closed]

开发者_如何学C As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

While I was learning a book, I came across a Java example. When I compiled it, I had no error. But when I ran it , it showed:

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

Since I am beginner in Java, I don't know what it is.

The program is:


import java.awt.*;
import java.awt.event.*;

class Party
{
public void buildInvite(){
    Frame f = new Frame();
    Label l = new Label("Party at Tim's");

    Button B = new Button("You Bet");
    Button c = new Button("Shoot me");
    Panel p = new Panel();
    p.add(l);
}
}


If you still want to see the execution of this code try this version:

class Party{
        public void buildInvite(){
        Frame f = new Frame();
        Label l = new Label("Party at Tim's");
        Button B = new Button("You Bet");
        Button c = new Button("Shoot me");
        Panel p = new Panel();
        p.add(l);
        p.add(B);
        p.add(c);
        f.add(p);
        f.setVisible(true);
        }

        public static void main(String[] args) {
            new Party().buildInvite();
        }
    }


This code can't be fixed with less work than a complete rewrite. It doesn't have a main method, cause the frame to show or add the panel to it. Find a better tutorial.


All Java programs must have a main method. This is what the JVM looks for to start your program.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜