gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:47:00 24 4
gpt4 key购买 nike

我正在尝试实现我使用 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>

最佳答案

我必须进行 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);
}

关于JavaFX - 打开新窗口时不加载 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com