2012-12-06 12 views
5

JavaFX Scene Builderを使用してJavaFXアプリケーションを構築しています。インターフェイスがシーンビルダで作成され、FXMLファイル(main.fxml)が作成されました。FXMLLoader.load()の使い方 - JavaFX 2

私のアプリケーションでインターフェイスを使用するには、FXMLLoaderを使用してFXMLファイルを読み込む必要がありますが、load()メソッドがObjectを返し、シーンを構築するために問題があります。

以下は私のMainClassの一部です。ページの型が親ではないため、コンパイラはエラーを与えている:

Object page = FXMLLoader.load(MainWindowController.class.getResource("main.fxml")); 
Scene scene = new Scene(page); 
primaryStage.setScene(scene); 
primaryStage.show(); 

私はこのコンパイルを行うと、正しく実行するために何をしますか?あなたは状態に必要FXMLを使ってロード対象のエンティティの

<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.paint.*?> 

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml"> 
    <children> 
    <VBox prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <children> 
     <MenuBar minHeight="21.0" prefHeight="21.0" prefWidth="600.0"> 
      <menus> 
      <Menu mnemonicParsing="false" text="File"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
       </items> 
      </Menu> 
      </menus> 
     </MenuBar> 
     <ToolBar minHeight="24.0"> 
      <items> 
      <Button fx:id="btFormat" mnemonicParsing="false" text="FORMAT" /> 
      <Button fx:id="btUnformat" mnemonicParsing="false" text="Unformat" /> 
      <Button fx:id="btAutoIndent" mnemonicParsing="false" text="Auto-indent" /> 
      <CheckBox fx:id="cbShowLineNumber" mnemonicParsing="false" text="Show line number" /> 
      </items> 
     </ToolBar> 
     <SplitPane dividerPositions="0.5" focusTraversable="true" orientation="VERTICAL" prefHeight="348.0" prefWidth="600.0"> 
      <items> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"> 
       <children> 
       <VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
        <children> 
        <Label text="INPUT - Paste your code here" /> 
        <TextArea fx:id="taInput" prefWidth="200.0" wrapText="true" /> 
        </children> 
       </VBox> 
       </children> 
      </AnchorPane> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"> 
       <children> 
       <VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
        <children> 
        <Label text="OUTPUT - The formatted code" /> 
        <TextArea fx:id="taOutput" prefWidth="200.0" wrapText="true" /> 
        </children> 
       </VBox> 
       </children> 
      </AnchorPane> 
      </items> 
     </SplitPane> 
     </children> 
    </VBox> 
    </children> 
</AnchorPane> 
+0

あなたの質問であなたの 'FXML'ソースを記載してくださいに役立つことを願っています。 – jewelsea

+0

FXMLファイルが追加されました。 – ceklock

答えて

8

のために様々な自分を入力し予想:ここ

はFXMLファイル(main.fxml)です。

Parent page = FXMLLoader.<Parent>load(MainWindowController.class.getResource("main.fxml").toExternalForm()); 
+0

あなたの答えをありがとう、それは私が思ったより簡単です。あなたの答えに基づいて、私はこれを好きでしたし、それも動作します: 親ページ=(親)FXMLLoader.load( MainWindowController.class.getResource( "main.fxml")); – ceklock

+4

Java> = 7の場合、キャストは必要ありません。 FXMLLoader.load(...)を使用してください。型推論は残りを行います。 – miho

5

この

Parent root= FXMLLoader.load(getClass().getResource("mention the path to your fxml file"); 

を試してみては、これは