Netbeans JavaFx Cant select Main class

不问归期 提交于 2020-06-27 06:25:18

问题


When I try to run my code in NetBeans I get a empty window saying "Browse JavaFX Application classes" but there are none to select. How can I solve this? Im trying to create a card game. This is the start to the main method.

import cardutils.Deck;
public class Main {

    public static void main(String[] args) {         
            Deck deck = new Deck();
            System.out.println(deck.toString());
    }
}

回答1:


If you want to make a JavaFX application, you need to create a class which extends the Application class of JavaFX and which also contains your main method. So something like this:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Deck extends Application {

   public static void main(String[] args) {
      Application.launch(args);
   }


   @Override
   public void start(Stage stage) {
      Group root = new Group(new Label("Hello JavaFX!"));
      Scene scene = new Scene(root, 1024, 786);
      stage.setScene(scene);
      stage.show();
   }
}

And then simply run the project and Netbeans will find this class because it contains a main method and will let you select it.

If, however, this project does not really need JavaFX (I just wondered because you're simply printing out something to the command line), you might have selected the wrong project type when creating your Netbeans project (JavaFX Project instead of normal Java Project). In this case create a new standard Java project and copy the code over from the old project.

EDIT: The list with "available classes" is empty because Netbeans can't find any classes that extend javafx.application.Application inside the JavaFX project you seem to have created.




回答2:


I had a similar issue and they seem to be caused by residual files under in the project folder. I erased the folder and everything in it, and recreated a new project. That solved the issue.




回答3:


I had the same problem. You may need to explicitly set source directory path. I fixed this problem by setting the source directory explicitly. File -> Project Properties -> Add Folder -> Browse your src directory. Build it and run again. I could run the application.



来源:https://stackoverflow.com/questions/54382835/netbeans-javafx-cant-select-main-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!