默认控制器无法使用我的fxml

时间:2018-03-24 22:57:13

标签: eclipse ubuntu javafx fxml scenebuilder

我不知道为什么这段代码不起作用。我使用Scene Builder创建了一个fxml文档,它所做的就是添加一个borderpane。到目前为止,我只是在尝试调用我的fxml时遇到错误。我想这可能是因为程序不认为我初始化了我的一些变量,但我不知道为什么。

/usr/sbin/sendmail -ti <<EOF
To: myself@gmail.com
From: myself@gmail.com
Subject: Test email
Date: Fri, 23 Mar 2018 22:26:38 0000 (GMT)

This is a test...

-- 
Myself
EOF

听到创建的fxml文件。     

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
import javafx.fxml.FXML;
import javafx.scene.layout.Pane;

public class SampleController {
    @FXML
    Pane mainPane;
    @FXML
    private void initialize() {
        System.out.println(mainPane.getHeight());
    }
}

这就是我的项目的样子。 [在此输入图像说明] [1]

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <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>
   </top>
   <center>
      <BorderPane prefHeight="200.0" BorderPane.alignment="CENTER">
         <center>
            <AnchorPane prefHeight="273.0" prefWidth="1104.0" BorderPane.alignment="CENTER">
               <children>
                  <Pane fx:id="taskPane" layoutX="10.0" layoutY="4.0" minHeight="259.0" AnchorPane.bottomAnchor="10.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <ScrollPane layoutX="19.0" layoutY="54.0" prefHeight="205.0" prefWidth="337.0">
                           <content>
                              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="235.0" prefWidth="290.0">
                                 <children>
                                    <VBox layoutX="1.0" prefHeight="238.0" prefWidth="297.0" />
                                 </children>
                              </AnchorPane>
                           </content>
                        </ScrollPane>
                        <Button layoutX="39.0" layoutY="16.0" mnemonicParsing="false" text="New Habit" />
                        <Label layoutX="152.0" layoutY="21.0" text="Habits">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane layoutX="376.0" layoutY="7.0" minHeight="259.0" AnchorPane.bottomAnchor="10.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <ScrollPane layoutX="19.0" layoutY="54.0" prefHeight="205.0" prefWidth="337.0">
                           <content>
                              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="235.0" prefWidth="290.0">
                                 <children>
                                    <VBox layoutX="1.0" prefHeight="238.0" prefWidth="297.0" />
                                 </children>
                              </AnchorPane>
                           </content>
                        </ScrollPane>
                        <Button layoutX="39.0" layoutY="16.0" mnemonicParsing="false" text="New Task" />
                        <Label layoutX="152.0" layoutY="21.0" text="Dayley Tasks">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane layoutX="748.0" layoutY="15.0" minHeight="259.0" prefHeight="259.0" prefWidth="356.0" AnchorPane.bottomAnchor="20.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <ScrollPane layoutX="19.0" layoutY="54.0" prefHeight="205.0" prefWidth="337.0">
                           <content>
                              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="235.0" prefWidth="290.0">
                                 <children>
                                    <VBox layoutX="1.0" prefHeight="238.0" prefWidth="297.0" />
                                 </children>
                              </AnchorPane>
                           </content>
                        </ScrollPane>
                        <Button layoutX="39.0" layoutY="16.0" mnemonicParsing="false" text="New ToDo" />
                        <Label layoutX="152.0" layoutY="21.0" text="To Do List">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
               </children>
               <padding>
                  <Insets bottom="10.0" />
               </padding>
            </AnchorPane>
         </center>
         <top>
            <Pane prefHeight="150.0" prefWidth="400.0" BorderPane.alignment="CENTER" />
         </top>
         <BorderPane.margin>
            <Insets left="10.0" right="10.0" top="10.0" />
         </BorderPane.margin>
      </BorderPane>
   </center>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

我想我已修复它尝试用这个

替换你的fxml
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <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>
   </top>
   <center>
      <BorderPane prefHeight="200.0" BorderPane.alignment="CENTER">
         <center>
            <AnchorPane prefHeight="273.0" prefWidth="1104.0" BorderPane.alignment="CENTER">
               <children>
                  <Pane fx:id="taskPane" layoutX="10.0" layoutY="4.0" minHeight="259.0" AnchorPane.bottomAnchor="10.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <ScrollPane layoutX="19.0" layoutY="54.0" prefHeight="205.0" prefWidth="337.0">
                           <content>
                              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="235.0" prefWidth="290.0">
                                 <children>
                                    <VBox layoutX="1.0" prefHeight="238.0" prefWidth="297.0" />
                                 </children>
                              </AnchorPane>
                           </content>
                        </ScrollPane>
                        <Button layoutX="39.0" layoutY="16.0" mnemonicParsing="false" text="New Habit" />
                        <Label layoutX="152.0" layoutY="21.0" text="Habits">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane layoutX="376.0" layoutY="7.0" minHeight="259.0" AnchorPane.bottomAnchor="10.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <ScrollPane layoutX="19.0" layoutY="54.0" prefHeight="205.0" prefWidth="337.0">
                           <content>
                              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="235.0" prefWidth="290.0">
                                 <children>
                                    <VBox layoutX="1.0" prefHeight="238.0" prefWidth="297.0" />
                                 </children>
                              </AnchorPane>
                           </content>
                        </ScrollPane>
                        <Button layoutX="39.0" layoutY="16.0" mnemonicParsing="false" text="New Task" />
                        <Label layoutX="152.0" layoutY="21.0" text="Dayley Tasks">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane layoutX="748.0" layoutY="15.0" minHeight="259.0" prefHeight="259.0" prefWidth="356.0" AnchorPane.bottomAnchor="20.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <ScrollPane layoutX="19.0" layoutY="54.0" prefHeight="205.0" prefWidth="337.0">
                           <content>
                              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="235.0" prefWidth="290.0">
                                 <children>
                                    <VBox layoutX="1.0" prefHeight="238.0" prefWidth="297.0" />
                                 </children>
                              </AnchorPane>
                           </content>
                        </ScrollPane>
                        <Button layoutX="39.0" layoutY="16.0" mnemonicParsing="false" text="New ToDo" />
                        <Label layoutX="152.0" layoutY="21.0" text="To Do List">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
               </children>
               <padding>
                  <Insets bottom="10.0" />
               </padding>
            </AnchorPane>
         </center>
         <top>
            <Pane fx:id="mainPane" prefHeight="150.0" prefWidth="400.0" BorderPane.alignment="CENTER" />
         </top>
         <BorderPane.margin>
            <Insets left="10.0" right="10.0" top="10.0" />
         </BorderPane.margin>
      </BorderPane>
   </center>