我无法使用FXML Loader阅读Tabpane

时间:2017-01-17 17:08:32

标签: java javafx javafx-2 fxml fxmlloader

我在乌克兰注册,很抱歉出错。 我想为FXML文件Tabpane设置一个基础,但是当我这样做时会出现异常:

 public Stage stage;
 private Scene scene;
 public void start(Stage stage) {
    try {
        q=(TabPane)FXMLLoader.load(Main.class.getResource("test.fxml"));
    } catch(Exception e) {
        e.printStackTrace();
    }
    scene=new Scene(q,640,480);
    stage.setScene(scene);
    stage.show();
}

但是当我想为AnchorPane程序设置基础时,写入时没有错误:

public void start(Stage stage) {
    try {
        pane=(AnchorPane)FXMLLoader.load(Main.class.getResource("test.fxml"));
    } catch(Exception e) {
        e.printStackTrace();
    }
    scene=new Scene(pane,640,480);
    stage.setScene(scene);
    stage.show();
}

这里有FXML文件 https://drive.google.com/open?id=0B5LBeiES5-shWDFLRnBPZUhhdmM

摘自FXML文件:

<TabPane fx:id="q" prefHeight="480.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <tabs>
      <Tab fx:id="q0">
         <content>
            <BorderPane prefHeight="546.0" prefWidth="752.0">
               <top>
                  <MenuBar prefHeight="25.0" prefWidth="432.0" BorderPane.alignment="CENTER">
                     <menus>
                        <Menu mnemonicParsing="false" text="File">
                          <items>
                              <MenuItem mnemonicParsing="false" onAction="#exitfile" text="Close" />
                           </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Options">
                           <items>
                              <Menu mnemonicParsing="false" text="Language">
                                 <items>
                                    <RadioMenuItem mnemonicParsing="false" text="EN" />
                                    <RadioMenuItem mnemonicParsing="false" text="UA" />
                                 </items>
                              </Menu>
                              <MenuItem mnemonicParsing="false" text="Others" />
                           </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Help">
                           <items>
                              <MenuItem mnemonicParsing="false" text="About" />
                           </items>
                    </Menu>
                    </menus>
                  </MenuBar>
               </top>
               <center>
                  <Pane prefHeight="501.0" prefWidth="735.0" BorderPane.alignment="CENTER">
                     <children>
                        <ImageView fitHeight="520.0" fitWidth="640.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../Test/res/fon.jpg" />
                           </image>
                        </ImageView>
                        <GridPane layoutX="216.0" layoutY="145.0" prefHeight="98.0" prefWidth="248.0">
                           <columnConstraints>
                              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                           </columnConstraints>
                           <rowConstraints>
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />    
                           </rowConstraints>
                           <children>
                              <Button fx:id="b1" disable="true" mnemonicParsing="false" onAction="#enblean" prefHeight="85.0" prefWidth="248.0"  text="Розпочати тест" GridPane.rowIndex="3" />
                             <Label alignment="CENTER" prefHeight="45.0" prefWidth="243.0" text="Введіть ім'я та прізвище">
                                 <font>
                                    <Font name="Lucida Fax Regular" size="14.0" />
                                 </font>
                              </Label>
                              <TextField fx:id="im" onAction="#b1dis"     onKeyPressed="#b1dis" onKeyReleased="#b1dis" onKeyTyped="#b1dis"  prefHeight="29.0" prefWidth="248.0" GridPane.rowIndex="1" />
                           </children>
                        </GridPane>
                     </children>
                  </Pane>
               </center>
            </BorderPane>
         </content>
      </Tab>    

FXML文件的结束

   </children>
                       </Pane>
                     </center>
                  </BorderPane>
               </children>
            </AnchorPane>
         </content>
      </Tab>
   </tabs>
</TabPane>

exeption:

javafx.fxml.LoadException: No controller specified.  
/C:/Users/Admin/workspace/Testp/bin/ua/NazarRepyanskiy/test.fxml:34  
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)  
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)  
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)  
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599 )  
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)  
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)  
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)  
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)  
at ua.NazarRepyanskiy.Main.start(Main.java:236)  
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)  
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)  
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)  
at java.security.AccessController.doPrivileged(Native Method)  
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)  
at  com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)  
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)  
at   com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)  
at java.lang.Thread.run(Unknown Source)  
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
at java.lang.reflect.Method.invoke(Unknown Source)  
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)  
at  com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)   
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
at java.lang.reflect.Method.invoke(Unknown Source)  
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)  
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)  
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)  
at java.lang.Thread.run(Unknown Source)  
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)  
at javafx.scene.Scene.<init>(Scene.java:223)  
at ua.NazarRepyanskiy.Main.start(Main.java:240)  
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)  
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)  
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)  
at java.security.AccessController.doPrivileged(Native Method)  
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)  
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)  
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)  
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)  
... 1 more  
Exception running application ua.NazarRepyanskiy.Main  

1 个答案:

答案 0 :(得分:0)

您不能使用值为onAction的{​​{1}}属性或以#exitfile开头的任何其他值,除非您使用#属性指定控制器fxml文件的根元素

fx:controller

或在用于加载fxml文件的控制器实例上使用<TabPane fx:controller="mypackage.ControllerClass" fx:id="q" prefHeight="480.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 指定控制器实例:

FXMLLoader.setController

(不能在fxml文件中指定FXMLLoader loader = new FXMLLoader(Main.class.getResource("test.fxml")); loader.setController(controllerInstance); q=(TabPane)loader.load(); 属性以使此方法起作用)

顺便说一句:在获得无法恢复的异常后,你不应该继续尝试。在这种情况下,这会导致打印2个堆栈跟踪:一个用于加载fxml文件时的异常,另一个用于通知您无法继续使用fx:controller作为q的根后

Scene

未向q=(TabPane)FXMLLoader.load(Main.class.getResource("test.fxml")); 分配非null值。更好的想法是将原始异常作为root引发异常,或者只是向q方法添加throws子句。

start