【问题标题】:Would not let me run the application不让我运行应用程序
【发布时间】:2014-10-08 20:14:43
【问题描述】:
package aaa;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.stage.Stage;

public class pear {

    public class aaa extends Application{
        @Override
        public void start(Stage primaryStage){
            Button okbt = new Button("ok");
            Scene scene = new Scene(okbt, 200,250);
            primaryStage.setTitle("n");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public static void main(String[] args){
            Application.launch(args);
        }
      }
    }

这是我的教科书为我提供的代码示例,我尝试运行它,但它无法运行。这是错误:

错误:在 aaa.pear 类中找不到主方法,请将主方法定义为: 公共静态无效主(字符串 [] 参数)。

我不明白为什么它错了,因为主类在起始类之外,在扩展应用程序之内。这直接来自一本书,我只需要知道它为什么无法运行。

【问题讨论】:

    标签: java


    【解决方案1】:

    static 方法只能在静态或顶级类中声明,而不能在非静态嵌套类中声明。在这种情况下,使用后者更简单

    public class MyPearApp extends Application {
    
       public static void main(String[] args){
        ...
       }
    }
    

    【讨论】:

      【解决方案2】:

      您的 main 方法位于内部类 (aaa) 中。请尝试将其直接放在 pear 类中。

      【讨论】:

      • 很高兴为您提供帮助。如果它解决了您的问题,请接受此答案。
      【解决方案3】:

      一切都应该在 main 方法中:

      例如:

      public class pear {
      
          public static void main(String[] args) {
      
          //Logic and Everything else goes in here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多