Java中出现错误,不知道我';我做错了
我是Java新手,我尝试在eclipse上执行以下操作:Java中出现错误,不知道我';我做错了,java,swing,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 ); fra
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\u world;
导入javax.swing.*;
公共类Hello_World扩展JFrame{
公共静态void main(字符串[]args){
JFrame=newhello_World();
框架。设置尺寸(300200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(“Hello world”);
frame.setVisible(true);
}
}
您的Hello\u World
类没有main
方法。您的main
在一个类中,在另一个类中。对于大多数外部类,Hello\u World
publicstaticvoidmain(String[]args)来说,它是不可见的{
这是主要的方法吗?有人能告诉我怎么做吗?你为什么需要一个静态的内部类呢?@user3552670,如果它对你有效,请在计时器结束后将其标记为可接受的答案。Merlin sure:D.公共静态类Vb0201扩展JFrame
那必须是静态的吗?@user3552670,一个内部类如果您试图在其封闭类的实例之外创建它的实例,则s必须是静态的。