样式表在javafx场景构建器中应用,但在运行应用程序时不应用

时间:2017-11-10 18:53:27

标签: javafx fxml

所以我有一个样式表,目前只有一种样式用于测试目的。

CSS代码:

.minimizeBtn{
-fx-background-color: rgba(0,0,0,1);
}

设置按钮的ID。这是舞台的控制器。

public void initialize(URL url, ResourceBundle rb) {
    handler = new DbHandlers();     

    minimizeBtn1.setId("minimizeBtn");

fxml中的我的按钮:

<Button fx:id="minimizeBtn1" layoutX="520.0" mnemonicParsing="false" 

prefHeight="25.0" prefWidth="12.0" styleClass="minimizeBtn" text="Button" />
            <Button layoutX="548.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="maximizeBtn" text="Button" />
            <Button layoutX="574.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="closeBtn" text="Button" />

我的fxml文件的其余部分:

    <AnchorPane prefHeight="450.0" prefWidth="600.0" style="-fx-background-color: #2A7FFF;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bownhrmain1.pages.LoginScreenController">
   <children>
      <AnchorPane prefHeight="450.0" prefWidth="600.0" style="-fx-background-color: #2A7FFF;">
         <children>
            <Label layoutX="335.0" layoutY="144.0" text="Inventory Systems" textFill="WHITE">
               <font>
                  <Font size="8.0" />
               </font>
            </Label>
            <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="200.0" layoutY="65.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../images/benx_logo.png" />
               </image>
            </ImageView>
            <Hyperlink layoutX="250.0" layoutY="368.0" text="Forgot Password?" textFill="WHITE" underline="true" />
            <JFXButton fx:id="loginBtn" buttonType="RAISED" layoutX="190.0" layoutY="302.0" prefHeight="51.0" prefWidth="221.0" ripplerFill="#2980b9" style="-fx-background-color: #1E5D87;" text="Login" textFill="WHITE" />
            <JFXPasswordField fx:id="passwordField" focusColor="#1e5d87" labelFloat="true" layoutX="190.0" layoutY="237.0" prefHeight="34.0" prefWidth="221.0" promptText="Password..." unFocusColor="WHITE" />
            <JFXTextField id="text-field" fx:id="usernameField" focusColor="#1e5d87" labelFloat="true" layoutX="190.0" layoutY="176.0" prefHeight="32.0" prefWidth="221.0" promptText="Username/Email..." unFocusColor="WHITE" />
            <ImageView fitHeight="20.0" fitWidth="19.0" layoutX="522.0" layoutY="5.0" pickOnBounds="true">
               <image>
                  <Image url="@../images/minimize.png" />
               </image>
            </ImageView>
            <ImageView fitHeight="11.0" fitWidth="12.0" layoutX="554.0" layoutY="9.0" pickOnBounds="true">
               <image>
                  <Image url="@../images/expand-button.png" />
               </image>
            </ImageView>
            <ImageView fitHeight="11.0" fitWidth="11.0" layoutX="581.0" layoutY="9.0" pickOnBounds="true">
               <image>
                  <Image url="@../images/closeWindow.png" />
               </image>
            </ImageView>
            <Button fx:id="minimizeBtn1" layoutX="520.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="minimizeBtn" text="Button" />
            <Button layoutX="548.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="maximizeBtn" text="Button" />
            <Button layoutX="574.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="closeBtn" text="Button" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

我在按钮上设置样式的原因是因为我尝试了通过类名和ID设置css。

这是在JavaFx场景构建器中工作的css的图像:

CSS Applying in scene builder. 这是运行程序时无法应用的图像:

Not applying

1 个答案:

答案 0 :(得分:1)

所以这个问题的答案是在你的启动函数中添加以下内容:

scene.getStylesheets().add(getClass().getResource("pathtostylesheet").toExternalForm());