JAVAFX - 在主fxml中加载带有anchorpane的fxml [英] JAVAFX - Load fxml with anchorpane inside main fxml

查看:1102
本文介绍了JAVAFX - 在主fxml中加载带有anchorpane的fxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在主fxml中加载一个fxml文件(内容区域)。

I'm trying to load a fxml file (content area) inside a main fxml.

由于代码被多个文件分割,我无法放在这里所有代码,但如果需要,我将构建一个问题的小版本。我只放置相关的代码部分。

As the code is splited by several files i can't place here all code but if needed i will build a small version with the problem. I only place the relevant parts of code.

主fxml文件

...
    </MenuBar>
    <AnchorPane fx:id="mainContent" prefHeight="-1.0" prefWidth="-1.0"    VBox.vgrow="ALWAYS" />
    <ToolBar prefHeight="25.0" VBox.vgrow="NEVER" />
  </children>
</VBox>


内容fxml (注意一下anchorpane id)

Content fxml (take in attention the anchorpane id)

 ...
 <AnchorPane id="background" prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
 <children>
   <Label minWidth="400.0" prefWidth="423.0" text="Tools" textAlignment="CENTER" textFill="WHITE" textOverrun="CLIP" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
  <effect>
    <Glow level="1.0" />
  </effect>
  <font>
    <Font name="Verdana Bold" size="45.0" />
  </font>
</Label>
<Label text="Version 0.01 alfa" textFill="WHITE" AnchorPane.rightAnchor="140.0" AnchorPane.topAnchor="69.0">
  <font>
    <Font name="Verdana" size="20.0" />
  </font>
</Label>
<Label text="2014 - José Longo" textFill="WHITE" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" />
 </children>
 <effect>
   <InnerShadow />
 </effect>
 </AnchorPane>

MainApp.java

MainApp.java

...
    @Override
public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader();
    AnchorPane mainScreen = (AnchorPane) loader.load(MainApp.class.getResource("/net/somewhere/fxml/Scene.fxml"));
    Scene scene = new Scene(mainScreen);
    scene.getStylesheets().add("/net/somewhere/styles/Styles.css");
    FXMLController mainController = loader.getController();

    ScreenNavigator.setMainControler(mainController);
    ScreenNavigator.setContent(ScreenNavigator.BACKGROUND); // if i remove/comment this the stage apears ok
    stage.setTitle("Tools");
    stage.setScene(scene);
    stage.show();
}

控制器类

public void setScreen(AnchorPane anchorPane){
    mainContent.getChildren().clear();
    mainContent.getChildren().addAll(anchorPane);
}

内容控制器

public class ScreenNavigator {

//fxml files
public static final String BACKGROUND = "/net/somewhere/fxml/Background.fxml";

private static FXMLController mainController;

public static void setMainControler(FXMLController mainController){
    ScreenNavigator.mainController = mainController;
}

public static void setContent(String fxmlFile){

    try {
        URL url = ScreenNavigator.class.getResource(fxmlFile);
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(url);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        AnchorPane content = (AnchorPane) loader.load(url.openStream());
        System.out.println(content.getId()); //not null the object  i can get the id 
        mainController.setScreen(content); // i get java.lang.NullPointerException at net.somewhere.controllers.ScreenNavigator.setContent(ScreenNavigator.java:40)
    } catch (IOException ex) {
        Logger.getLogger(ScreenNavigator.class.getName()).log(Level.SEVERE, null, ex);
    }

}
}

当尝试运行我得到这个错误:

And when try to run i get this error:

 --- exec-maven-plugin:1.2.1:exec (default-cli) @ Tools ---

background  <---- i can get the id of anchorpane
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
 Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
 Caused by: java.lang.NullPointerException
at net.somewhere.controllers.ScreenNavigator.setContent(ScreenNavigator.java:40) <----
at net.somewhere.MainApp.start(MainApp.java:30)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more

我已尝试使用带有Node,Pane等的getResource(),getResourceAsStream()...最后以此处的示例结束这个网站,但没有成功...总是得到这种类型的错误...

I have tried with getResource(), getResourceAsStream() with Node, Pane, etc... and finally ended with a example present here at this site, but without success... always get this type of error...

有人能指出我解决这个问题的方向吗?

Can someone point me a direction to solve this problem?

TIA

JL

推荐答案

在与代码进行一些争斗后,我终于找到了解决方案...

After some "fights" with the code i finally found a solution...

对于那些正在寻找方式的人,我提供了一个完整的示例netbeans项目您可以这里

For the ones as me who are looking for a way i provide a full example netbeans project you can get here

简单工作示例的屏幕:

Screens of simple working example:

我无法发布图片...
链接

I can't post images yet... links for them

这里

如果你去Hel p->关于您将获得以下屏幕上的帮助内容

If you go to "Help-> About" you will get the help content as on screen below

评论链接...

如果您点击此屏幕上的关闭按钮,您将获得初始屏幕。

If you hit button "Close" on this screen, you will get initial screen.

问候

JL

这篇关于JAVAFX - 在主fxml中加载带有anchorpane的fxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆