0

我写了以下代码:

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);
    } //more code here...
}

我不断收到以下错误:Exception in thread "main" java.lang.NoSuchMethodError: main

我试图main (String[] args)在代码中添加一个额外的内容,但我仍然遇到同样的错误。

我还应该在代码中添加什么以及它应该放在布局中的什么位置?

4

2 回答 2

4

确保您的main方法如下所示:

public static void main(String[] args) {
   ...
}
于 2011-09-15T16:49:15.873 回答
3

Java 和 Swing 的初学者应该查看Oracle 教程。这是他们的Swing Hello World 程序

package start;

import javax.swing.*;        

public class HelloWorldSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }});}}
于 2011-09-15T16:53:22.353 回答