2012-12-06 18 views
5

का उपयोग कैसे करें मैं JavaFX Scene Builder का उपयोग कर JavaFX एप्लिकेशन बना रहा हूं। इंटरफ़ेस दृश्य बिल्डर में बनाया गया था और FXML फ़ाइल (main.fxml) बनाया गया था।FXMLLoader.load() - JavaFX 2

मेरे आवेदन में इंटरफ़ेस का उपयोग करने के लिए मुझे FXMLLoader का उपयोग करके FXML फ़ाइल लोड करनी होगी, लेकिन एक समस्या है क्योंकि लोड() विधि ऑब्जेक्ट लौटाती है, और एक दृश्य बनाने के लिए मुझे अभिभावक वर्ग का एक उदाहरण चाहिए।

नीचे मेरे मेनक्लास का एक टुकड़ा है। संकलक एक त्रुटि दे रहा है क्योंकि पृष्ठ प्रकार जनक की नहीं है:

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

जावा के लिए> = 7 कास्ट आवश्यक नहीं है। बस उपयोग करें: FXMLLoader.load (...)। टाइप अनुमान बाकी करेंगे। – miho

5

इस

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

आशा व्यक्त की कि इस में मदद मिलेगी