[英]Getting error in Java, Have no idea what I'm doing wrong
我是 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
有人能告诉我我做错了什么吗?
编译器抱怨是因为您在嵌套类中定义了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 );
}
}
这是一个更好的解决方案:
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 );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.