在Java中获取错误,不知道我做错了什么

时间:2014-04-19 21:17:26

标签: java swing

我是Java新手,我试图在eclipse上做以下事情:

import javax.swing.*;

public class Hello_World {
  public class HelloWorld extends JFrame
  {  
     public static void main(String[] args) {
     JFrame frame = new HelloWorld();
     frame.setSize( 300, 200 );
     frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     frame.setTitle( "Hello world" );
     frame.setVisible( true );
    }
  }
}

我不知道我在这里做错了什么。 编译器给出了以下错误:

Main method not found in class Hello_World, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application 

有人能告诉我我做错了吗?

2 个答案:

答案 0 :(得分:0)

编译器抱怨,因为您已在嵌套类中定义了main方法,而不是直接在您正在编译的类中。

只需将main方法移至HelloWorld类。

import javax.swing.*;
public class Hello_World {
    public static class HelloWorld extends JFrame
    {  

    }
    public static void main(String[] args) {
        JFrame frame = new HelloWorld();
        frame.setSize( 300, 200 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Hello world" );
        frame.setVisible( true );
    }
}

答案 1 :(得分:0)

这是一个更好的解决方案:

package hello_world;

import javax.swing.*;

public class Hello_World extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new Hello_World();
        frame.setSize( 300, 200 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Hello world" );
        frame.setVisible( true );
    }
}