JavaFX:没有错误加载fxml但是whitescreen

时间:2018-02-01 13:34:40

标签: java css javafx jfoenix

我的项目没有编译错误。但我的程序仍然打开并保持白色而不是使用css文件。我尝试手动添加样式表,如下面的代码示例所示。

有人有个主意吗?

设置:

Java 8 SceneBuilder FontAwesomefx 8.9 jfoenix 8.0.1

主要课程样本:

  FXMLLoader loader = new FXMLLoader();
        URL loc_login = Main.class.getResource("/fxml/loginscreen.fxml");
        loader.setLocation(loc_login);
        this.rootLayout = loader.load();
        Scene scene = new Scene(rootLayout);
        scene.getStylesheets().clear();
        scene.getStylesheets().add(getClass().getResource("/styles/dark-theme.css").toString());
        primaryStage.setScene(scene);
        primaryStage.centerOnScreen();
        primaryStage.show();

和fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<AnchorPane styleClass="root" blendMode="SCREEN" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="400.0" stylesheets="@../styles/dark-theme.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <JFXPasswordField fx:id="password" focusColor="WHITE" layoutX="88.0" layoutY="148.0" prefHeight="26.0" prefWidth="250.0" promptText="Password" />
      <JFXTextField fx:id="username" focusColor="WHITE" layoutX="88.0" layoutY="95.0" prefHeight="26.0" prefWidth="250.0" promptText="Username" />
      <FontAwesomeIconView glyphName="LOCK" layoutX="62.0" layoutY="170.0" size="25" />
      <FontAwesomeIconView glyphName="USER" layoutX="62.0" layoutY="118.0" size="25" styleClass="glyph-icon" />
      <JFXButton layoutX="118.0" layoutY="217.0" prefHeight="30.0" prefWidth="84.0" text="S'inscrire" underline="true" />
      <JFXButton layoutX="215.0" layoutY="217.0" prefHeight="30.0" prefWidth="84.0" text="Login" underline="true" />
      <ImageView fitHeight="51.0" fitWidth="67.0" layoutX="99.0" layoutY="14.0">
         <image>
            <Image url="@../img/main-icon-penelope.png" />
         </image>
      </ImageView>
      <Label layoutX="160.0" layoutY="13.0" prefHeight="51.0" prefWidth="139.0" styleClass="app-label" text="MY PENELOPE F" textAlignment="CENTER" textFill="WHITE" />
   </children>
   <effect>
      <DropShadow />
   </effect>
   <cursor>
      <Cursor fx:constant="HAND" />
   </cursor>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

我认为您需要在fx:controller:<class name>行的fxml文件中添加Anchorpane,并将<class name>替换为加载fxml的类的名称。

这应该有用。