Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在eclipse中使用JavaFX从另一个项目加载FXML文件?_Java_Eclipse_Javafx_Fxml_Scene - Fatal编程技术网

如何在eclipse中使用JavaFX从另一个项目加载FXML文件?

如何在eclipse中使用JavaFX从另一个项目加载FXML文件?,java,eclipse,javafx,fxml,scene,Java,Eclipse,Javafx,Fxml,Scene,我有两个项目:“游戏”和“游戏库”-游戏有一个游戏库项目的构建路径。在游戏库中,我有一个方法可以在游戏请求时切换场景 游戏库项目中的场景切换方法: package gamelibrary; public class SceneSwitcher{ public Stage window; public Parent root; public void switchSceneFXML(String FXMLLocation) throws Exception{

我有两个项目:“游戏”和“游戏库”-游戏有一个游戏库项目的构建路径。在游戏库中,我有一个方法可以在游戏请求时切换场景

游戏库项目中的场景切换方法:

package gamelibrary;

public class SceneSwitcher{

    public Stage window;
    public Parent root;

    public void switchSceneFXML(String FXMLLocation) throws Exception{

        try{

            window = (Stage) node.getScene().getWindow();

        }catch(Exception e){

            e.printStackTrace();

        }

        root = FXMLLoader.load(getClass().getResource(FXMLLocation));

        window.setScene(new Scene(root));
        window.show();

    }
}
我在游戏项目中称此方法为:

package game;

import gamelibrary.sceneSwitcher;

public class firstWindowController{

    public void buttonHandler(ActionEvent event){

        SceneSwitcher scene = new SceneSwitcher();

        if(event.getSource().equals(playButton){

            scene.switchScene("/game/SecondWindow.fxml");

        }

    }

}
因此,当我单击FirstWindow.fxml中的playButton时,游戏项目中的FirstWindowController将调用游戏库项目中的switchSceneFXML方法

我得到这个错误:

java.lang.NullPointerException:需要位置

有什么我遗漏的吗?如何让switchSceneFXML方法知道在游戏项目中查找包和文件,而不是在游戏库项目中查找

我试图使switchSceneFXML方法可与其他FXML文件一起重用,这样我就不必大量复制和粘贴代码,并且更喜欢一种允许我编写一次并重用的方法

我到处寻找答案,但在其他人的情况下,我还不能完全理解这个概念。谢谢你的帮助

我试图使switchSceneFXML方法可重用的范围不仅仅限于FXML。有没有一种方法可以自动获取类的名称克里斯

然后您应该让各个类自己加载它们的FXML。您可以通过使用JVM来实现这一点

在gane lib中:

interface FxmlController{
  boolean isPrividingScene(String nextSceneIdOrName);
  String getFXMLLocation();
}


public class SceneSwitcher{
    public Stage window;
    public Parent root;
   private static ServiceLoader<FxmlController> sceneControllers
     = ServiceLoader.load(FxmlController.class);
    public void switchSceneFXML(String nextSceneIdOrName) throws Exception{
        try{
            window = (Stage) node.getScene().getWindow();
            for(FxmlController controller : sceneControllers){
               if(controller.isPrividingScene(nextSceneIdOrName)){ // however you want to select the next scene...
                 FXMLLoader fxmlLoader = new FXMLLoader();
                 fxmlLoader.setController(controller);
                 root = fxmlLoader.load(controller.getClass().getResource(controller.getFXMLLocation()));
                 window.setScene(new Scene(root));
                 window.show();
               }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
接口控制器{
布尔值isPrivingScene(字符串nextSceneIdOrName);
字符串getFXMLLocation();
}
公共类场景切换程序{
公共舞台窗口;
公共父根;
专用静态ServiceLoader场景控制器
=ServiceLoader.load(FxmlController.class);
public void switchSceneFXML(字符串nextSceneIdOrName)引发异常{
试一试{
window=(Stage)node.getScene().getWindow();
用于(FxmlController:sceneControllers){
如果(controller.isPrivingScene(nextSceneIdOrName)){//但是您想选择下一个场景。。。
FXMLLoader FXMLLoader=新的FXMLLoader();
fxmlLoader.setController(控制器);
root=fxmlLoader.load(controller.getClass().getResource(controller.getFXMLLocation());
window.setScene(新场景(根));
window.show();
}
}
}捕获(例外e){
e、 printStackTrace();
}
}
}

通过这种方式,每个FxmlController可以(但不需要)生活在自己的jar文件中,该文件具有自己到FXML文件的相对路径。

“/companytoon/launchegui.fmxl”
fmxl
而不是
FXML
)问题中的输入错误,或者是
FXMLLoader.load(getClass().getResource(FXMLLocation))中的代码中的输入错误
您必须对另一个项目中的类调用
getClass()
fxmloader.load(ClassInOtherProject.class.getResource(FXMLLocation))
@James\D是的,这是一个打字错误。我纠正了这一点。@TimothyTruckle我试图使switchSceneFXML方法可重用于不止一个FXML。有没有一种方法可以让我自动获得类的名称?@Chris就是这样:在最终发行版中,单独的项目通常以单独的jar结尾……谢谢。我将对此进行一次尝试。您必须将包含控制器和FXML的项目添加到游戏库项目的运行时类路径(在运行配置中)才能使其工作。几年后,我的回答是“是”,事实上,这确实工作(在同一个项目中-而不是单独的JAR中)-但我花了几个月的时间耐心地自己想出来。。。它起作用了;我使用了这个答案提供的大部分代码,但我没有使用ServiceLoader。。。(不知道为什么)后来我真正遇到的唯一问题是,当我切换场景时,舞台将换成另一个线程。。。可能会使它成为一种无用的方法。现在,我正在进步,决定放弃JavaFX,将普通的Java Swing集成到我当前的项目中。。。岁月流逝,但仍在我心中。
scene.switchSceneFXML("/game/SecondWindow.fxml");
interface FxmlController{
  boolean isPrividingScene(String nextSceneIdOrName);
  String getFXMLLocation();
}


public class SceneSwitcher{
    public Stage window;
    public Parent root;
   private static ServiceLoader<FxmlController> sceneControllers
     = ServiceLoader.load(FxmlController.class);
    public void switchSceneFXML(String nextSceneIdOrName) throws Exception{
        try{
            window = (Stage) node.getScene().getWindow();
            for(FxmlController controller : sceneControllers){
               if(controller.isPrividingScene(nextSceneIdOrName)){ // however you want to select the next scene...
                 FXMLLoader fxmlLoader = new FXMLLoader();
                 fxmlLoader.setController(controller);
                 root = fxmlLoader.load(controller.getClass().getResource(controller.getFXMLLocation()));
                 window.setScene(new Scene(root));
                 window.show();
               }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}