简体   繁体   English

在 Java 中出错,不知道我做错了什么

[英]Getting error in Java, Have no idea what I'm doing wrong

I'm new to Java and I was trying to do the following on eclipse:我是 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 );
    }
  }
}

I have no idea what I doing wrong here.我不知道我在这里做错了什么。 The compiler gives me following error:编译器给了我以下错误:

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 

Can someone tell me what I'm doing wrong?有人能告诉我我做错了什么吗?

The compiler is complaining because you have defined your main method inside a nested class, instead of directly in the class that you are compiling.编译器抱怨是因为您在嵌套类中定义了main方法,而不是直接在您正在编译的类中定义。

Just move the main method into the HelloWorld class.只需将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 );
    }
}

Here is a better solution:这是一个更好的解决方案:

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.

相关问题 我不知道我在做什么-绘画方法等 - I have no idea what I'm doing - Paint Methods, etc 在InventoryPart 2程序上运行的Java代码,不知道我在做什么错吗? - Java code working on InventoryPart 2 program, have no idea what I am doing wrong? Java:尝试调用shooterExperiments方法时,我总是收到错误消息。 我不确定我在做什么错或确切如何调用方法 - Java: I keep getting an error when trying to call the shooterExperiments method. I'm not sure what I'm doing wrong or exactly how to call a method 我收到看似简单的 Java 代码的奇怪错误。 我在这里做错了什么? - I'm getting weird errors for seemingly simple Java code. What am I doing wrong here? 我的外键没有插入数据库,知道我在做什么错吗? - My foreign key isn't being inserted into the database, any idea what I'm doing wrong? “线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException”。 我不知道我在做什么错 - “Exception in thread ”AWT-EventQueue-0“ java.lang.NullPointerException”. I have no clue what I'm doing wrong 我被要求实现一个基于树的地图,却不知道我在做什么 - I've been asked to implement a Tree based Map, and have no idea what I'm doing 我在此算法上做错了什么? - what i'm i doing wrong on this algorithm? Java:indexOf和put方法-我不确定这些方法在做什么 - Java: indexOf and put methods - I'm not sure what I'm doing wrong in these methods Java 练习:我不确定我做错了什么,感谢所有帮助 - Java Exercise: I'm not sure what I'm doing wrong and all help is appreciated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM