2011-09-15 21 views
1

我写了下面的代码:最小的Java程序应该如何?

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)添加到代码,但我仍然得到同样的错误。

还有什么我应该放在代码和布局应该去哪里?

+0

既然你自称是一个绝对的菜鸟,请确保您使用_javac filename.java_发行_JAVA filename_ – Jagat

+0

@Jagat是的,我得到了部分权利之前编译Java类。大声笑。不过谢谢。 –

回答

3

确保您main方法是这样的:

public static void main(String[] args) { 
    ... 
} 
3

初学者使用Java和Swing应该看看Oracle tutorial。这里是他们的Swing Hello World program

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(); 
      }});}} 
+0

谢谢。我一定会看看Oracle教程。 –

+0

@Craig:你可以运行很多很好的示例代码。另外如果你想要一本deadtree书籍,Core Java(http://www.horstmann.com/corejava.html)是一个开始Swing的好地方。 –