如何在JavaFX应用程序中写入FXML文件的路径?

时间:2018-12-27 08:39:24

标签: java javafx path fxml

我试图在主类中编写路径以加载FXML文件MenuFXML.fxml,但是我不知道这样做的正确方法。 FXML文件位于软件包com.developer.fxml中,而主类位于com.developer中。

我已经尝试过路径/com/developer/fxml/MenuFXML.fxml/fxml/MenuFXML.fxml,但是它们都不起作用。

package com.developer;

import com.developer.exit.ExitApplication;
import com.developer.util.css.AddCSS;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    private Stage stage;
    private Scene scene;
    private final String title = "Stock Program 2.0";
    private Parent root;

    private final AddCSS addcss = new AddCSS();

    @Override
    public void start(Stage primaryStage) throws IOException {

        stage = primaryStage;
        stage.setTitle(title);

        stage.setOnCloseRequest(e -> {
            e.consume();
            ExitApplication ep = new ExitApplication();
            ep.closeProgram(stage);
        });

        //The part that I can't get to work
        root = FXMLLoader.load(getClass().getResource("/fxml/MenuFXML.fxml"));

        scene = new Scene(root);
        addcss.setStylesheet(scene);
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

0 个答案:

没有答案