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

bluestorm 阅读:33 2023-09-06 19:40:43 评论:0

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

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> 

请您参考如下方法:

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

1.首先将所有图片放入Style.css所在文件夹

2.其次,添加这几行代码:

String css = this.getClass().getResource("Style.css").toExternalForm(); scene.getStylesheets().add(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); 
        } 


标签:CSS
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号