JavaFX - 打开新窗口时不加载CSS

时间:2016-03-29 21:32:40

标签: java javafx

我正在尝试使用Scene Builder实现相同的界面。但由于某种原因,CSS没有加载。 这就是我应该得到的! enter image description here 这就是我得到的! enter image description here

Parent root;

    try {

    root = FXMLLoader.load(getClass().getResource("/path../path../TutorControlPanel.fxml"));

    Stage stage = new Stage();

    stage.setTitle("Tutor Control Panel");

    stage.setScene(new Scene(root, 950, 750));

    stage.show();

         } catch (IOException ex) {
             Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
                       }
                } else {
                      System.out.println("user not found");

                        }

                        }

               } catch (SQLException ex) {
                 Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);                     }

XML从这里开始!

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

<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.media.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox fx:id="root" prefHeight="750.0" prefWidth="950.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">


   <children>
      <HBox prefHeight="100.0" prefWidth="200.0" style="-fx-background-color: #353f40;">
         <children>
            <ImageView>
               <image>
                  <Image url="@../../../Images/Accuranture.png" />
               </image>
            </ImageView>
            <GridPane fx:id="gridView" prefHeight="150.0" prefWidth="688.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="71.0" minHeight="10.0" prefHeight="36.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="120.0" minHeight="10.0" prefHeight="114.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Button fx:id="recordButton" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="70.0" prefWidth="70.0" stylesheets="@Style.css" textFill="WHITE" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets bottom="15.0" left="15.0" right="15.0" top="10.0" />
                     </GridPane.margin>
                     <padding>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </padding>
                     <tooltip>
                        <Tooltip text="Screencast Lessons">
                           <font>
                              <Font size="16.0" />
                           </font>
                        </Tooltip>
                     </tooltip>
                  </Button>
                  <Button fx:id="stopButton" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="70.0" prefWidth="70.0" stylesheets="@Style.css" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets bottom="100.0" left="100.0" right="100.0" top="100.0" />
                     </GridPane.margin>
                     <padding>
                        <Insets top="10.0" />
                     </padding>
                  </Button>
                  <Label text="Record" textFill="WHITE" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets bottom="100.0" left="20.0" right="90.0" top="200.0" />
                     </GridPane.margin>
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                  </Label>
                  <Label layoutX="30.0" layoutY="141.0" text="Stop" textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets bottom="5.0" left="115.0" right="40.0" top="255.0" />
                     </GridPane.margin>
                  </Label>
                  <MenuButton fx:id="settingsButton" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="70.0" prefWidth="70.0" stylesheets="@Style.css" GridPane.rowIndex="1">
                    <items>
                      <MenuItem fx:id="qualitySettings" mnemonicParsing="false" text="Quality" />
                      <MenuItem fx:id="videoSettings" mnemonicParsing="false" text="Speed" />
                    </items>
                     <GridPane.margin>
                        <Insets bottom="14.0" left="190.0" right="100.0" top="15.0" />
                     </GridPane.margin>
                     <opaqueInsets>
                        <Insets />
                     </opaqueInsets>
                  </MenuButton>
                  <Label layoutX="125.0" layoutY="141.0" text="Settings" textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="190.0" top="250.0" />
                     </GridPane.margin>
                  </Label>
                  <Button fx:id="lessoncategory" layoutX="110.0" layoutY="65.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="55.0" prefWidth="70.0" stylesheets="@Style.css">
                     <padding>
                        <Insets top="10.0" />
                     </padding>
                     <GridPane.margin>
                        <Insets left="310.0" top="155.0" />
                     </GridPane.margin>
                  </Button>
                  <Label layoutX="200.0" layoutY="141.0" text="Create Category" textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="285.0" top="250.0" />
                     </GridPane.margin>
                  </Label>
                  <MenuButton fx:id="serverUpload" layoutX="200.0" layoutY="66.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="70.0" prefWidth="70.0" stylesheets="@Style.css">
                     <items>
                        <MenuItem fx:id="startUpload" mnemonicParsing="false" text="Quality" />
                        <MenuItem fx:id="stopUpload" mnemonicParsing="false" text="Speed" />
                     </items>
                     <opaqueInsets>
                        <Insets />
                     </opaqueInsets>
                     <GridPane.margin>
                        <Insets left="435.0" top="145.0" />
                     </GridPane.margin>
                  </MenuButton>
                  <Label layoutX="295.0" layoutY="141.0" text="Server Uploads" textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="420.0" top="250.0" />
                     </GridPane.margin>
                  </Label>
                  <Button fx:id="timer" layoutX="25.0" layoutY="63.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="70.0" prefWidth="70.0" stylesheets="@Style.css">
                     <padding>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </padding>
                     <tooltip>
                        <Tooltip text="Screencast Lessons">
                           <font>
                              <Font size="16.0" />
                           </font>
                        </Tooltip>
                     </tooltip>
                     <GridPane.margin>
                        <Insets left="550.0" top="145.0" />
                     </GridPane.margin>
                  </Button>
                  <Label fx:id="timerHours" layoutX="430.0" layoutY="141.0" text="00 : " textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="550.0" top="250.0" />
                     </GridPane.margin>
                  </Label>
                  <Label fx:id="timerMinutes" layoutX="560.0" layoutY="141.0" text=" 00 : " textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="575.0" top="250.0" />
                     </GridPane.margin>
                  </Label>
                  <Label fx:id="timerSeconds" layoutX="585.0" layoutY="141.0" text="  00 " textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="600.0" top="250.0" />
                     </GridPane.margin>
                  </Label>
                  <Label layoutX="560.0" layoutY="141.0" text="Timer" textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="565.0" top="220.0" />
                     </GridPane.margin>
                  </Label>
                  <Button fx:id="logOut" mnemonicParsing="false" prefHeight="26.0" prefWidth="25.0" stylesheets="@Style.css">
                     <GridPane.margin>
                        <Insets left="650.0" />
                     </GridPane.margin>
                  </Button>
                  <Label layoutX="30.0" layoutY="144.0" text="Log Out" textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="590.0" />
                     </GridPane.margin>
                  </Label>
                  <Label layoutX="30.0" layoutY="144.0" text="Welcome Back " textFill="WHITE">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets />
                     </GridPane.margin>
                  </Label>
                  <Label fx:id="uNameLabel" layoutX="10.0" layoutY="19.0" text="User" textFill="LAWNGREEN">
                     <font>
                        <Font name="System Bold" size="14.0" />
                     </font>
                     <GridPane.margin>
                        <Insets left="120.0" />
                     </GridPane.margin>
                  </Label>
               </children>
            </GridPane>
         </children>
      </HBox>
      <SplitPane dividerPositions="0.32293986636971045" style="-fx-background-color: ;" VBox.vgrow="ALWAYS">
         <items>
            <ListView prefHeight="200.0" prefWidth="200.0" />
            <Pane prefHeight="200.0" prefWidth="200.0">
               <children>
                  <MediaView fitHeight="500.0" fitWidth="575.0" layoutX="15.0" layoutY="24.0" />
               </children>
            </Pane>
         </items>
      </SplitPane>
   </children>
</VBox>

1 个答案:

答案 0 :(得分:1)

我必须进行2次更改才能获得预期的结果

1.首先,将所有图像放入Style.css

的文件夹中

2.其次,添加以下代码行:

String css = this.getClass()。getResource(&#34; Style.css&#34;)。toExternalForm();                           。scene.getStylesheets()添加(CSS);

     try {

    Parent root=FXMLLoader.load(getClass().getResource("/eLearning/UserInterface/TutorControlPanel.fxml"));
    Scene scene = new Scene(root);

      String css = this.getClass().getResource("Style.css").toExternalForm();
                        scene.getStylesheets().add(css);
                        Stage stage = new Stage();

                        stage.setTitle("Tutor Control Panel");

                        stage.setScene(scene);
                        stage.show();

                    } catch (IOException ex) {
                        Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    System.out.println("user not found");

                }

            }

        } catch (SQLException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
        }