我是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
有人能告诉我我做错了吗?
答案 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 );
}
}