我正在尝试将CSS样式表添加到我的应用程序中 . 我正在使用场景构建器,当我将样式表连接到场景构建器时,它显示正常 . 但是,当我运行我的程序时,没有样式 . 我正在使用舞台监督来切换舞台 . 我真的不确定在我的应用程序中加载CSS的位置 . CSS样式表已经在我的FXML文件中连接 . 当我尝试在我的舞台管理器中加载CSS时(使用scene.getStylesheets() . add(getClass() . getResource(“../../ java / MainCSS.css”) . toExternalForm());) - ,我得到一个空指针异常 .
项目布局:pic of layout项目布局:pic of layout pt 2
Stage Manager
package project_3368.project_3368.view;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.slf4j.Logger;
import java.util.Objects;
import static org.slf4j.LoggerFactory.getLogger;
public class StageManager {
private static final Logger LOG = getLogger(StageManager.class);
private final Stage primaryStage;
private final SpringFXMLLoader springFXMLLoader;
public StageManager(SpringFXMLLoader springFXMLLoader, Stage stage){
this.springFXMLLoader = springFXMLLoader;
this.primaryStage = stage;
}
public void switchScene(final FxmlView view){
Parent viewRootNodeHierarchy = loadViewNodeHierarchy(view.getFxmlFile());
show(viewRootNodeHierarchy, view.getTitle());
}
private void show(final Parent rootnode, String title){
Scene scene = prepareScene(rootnode);
scene.getStylesheets().add(getClass().getResource("../../java/MainCSS.css").toExternalForm());
primaryStage.setTitle(title);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
try{
primaryStage.show();
}catch (Exception exception){
logAndExit("Unable to show scene for title" + title, exception);
}
}
private Scene prepareScene(Parent rootnode){
Scene scene = primaryStage.getScene();
if(scene == null){
scene = new Scene(rootnode);
}
scene.setRoot(rootnode);
return scene;
}
private Parent loadViewNodeHierarchy(String fxmlFilePath){
Parent rootNode = null;
try{
rootNode = springFXMLLoader.load(fxmlFilePath);
Objects.requireNonNull(rootNode, "A root FXML node must not be null");
}catch (Exception exception){
logAndExit("Unable to load FXML view" + fxmlFilePath, exception);
}
return rootNode;
}
private void logAndExit(String errorMsg, Exception exception){
LOG.error(errorMsg, exception, exception.getCause());
Platform.exit();
}
}
Spring FXML Loader
package project_3368.project_3368.view;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ResourceBundle;
@Component
public class SpringFXMLLoader {
private final ResourceBundle resourceBundle;
private final ApplicationContext context;
@Autowired
public SpringFXMLLoader(ApplicationContext context, ResourceBundle resourceBundle){
this.resourceBundle = resourceBundle;
this.context = context;
}
public Parent load(String fxmlPath) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(context::getBean);
loader.setResources(resourceBundle);
loader.setLocation(getClass().getResource(fxmlPath));
return loader.load();
}
}
Project3368Application
package project_3368.project_3368;
import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import project_3368.project_3368.view.FxmlView;
import project_3368.project_3368.view.StageManager;
@SpringBootApplication
public class Project3368Application extends Application {
protected ConfigurableApplicationContext springContext;
protected StageManager stageManager;
public static void main(final String[] args) {
Application.launch(args);
}
@Override
public void init() throws Exception {
springContext = springBootApplicationContext();
}
@Override
public void start(Stage stage) throws Exception {
stageManager = springContext.getBean(StageManager.class, stage);
displayInitialScene();
}
@Override
public void stop() throws Exception{
springContext.close();
}
protected void displayInitialScene(){stageManager.switchScene(FxmlView.CUSTOMER);}
private ConfigurableApplicationContext springBootApplicationContext() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Project3368Application.class);
String[] args = getParameters().getRaw().stream().toArray(String[]::new);
builder.headless(false);
return builder.run(args);
}
}
MainCSS.css
.anchor {
-fx-background-image:url('Red.jpg');
-fx-background-size: cover;
-fx-background-repeat: no-repeat;
}
.button{
-fx-background-color:#FFFFFF;
-fx-opacity: 1;
-fx-text-fill: black;
}
.button:hover{
-fx-background-color: #bcbcbc ;
}
.label{
-fx-text-fill: black;
-fx-font-size: 15;
-fx-font-weight: bold;
-fx-background-color: white;
}
.table-view .column-header .label {
-fx-text-fill: black;
-fx-font-weight: bold;
-fx-font-size: 12;
}
.table-view .column-header{
-fx-background-color: black;
-fx-opacity: 0.7;
}
.table-view{
-fx-opacity: 0.8;
}
.text-field{
-fx-opacity: 0.7;
}
kanban.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="800.0" styleClass="anchor" stylesheets="@../../java/MainCSS.css" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="project_3368.project_3368.Controllers.KanbanController">
<children>
<ListView fx:id="ProductionList" layoutX="387.0" layoutY="81.0" onDragDetected="#dragProductionDetected" onDragDone="#DragDoneOnProduction" onDragDropped="#dragDroppedOnTarget" onDragOver="#dragOverTarget" prefHeight="274.0" prefWidth="200.0" />
<ListView fx:id="PreproductionList" layoutX="190.0" layoutY="81.0" onDragDetected="#dragSourceDetected" onDragDone="#DragDoneOnSource" prefHeight="274.0" prefWidth="200.0" />
<Label layoutX="190.0" layoutY="59.0" prefHeight="21.0" prefWidth="200.0" text=" Pre-Production" textAlignment="CENTER">
<font>
<Font name="Calibri" size="12.0" />
</font></Label>
<Label layoutX="387.0" layoutY="59.0" prefHeight="21.0" prefWidth="200.0" text=" Production" textAlignment="CENTER" />
<ListView fx:id="PostProductionList" layoutX="586.0" layoutY="81.0" onDragDropped="#dragDroppedOnPostProduction" onDragOver="#dragOverPostProduction" prefHeight="274.0" prefWidth="184.0" />
<Label layoutX="586.0" layoutY="59.0" prefHeight="21.0" prefWidth="184.0" text=" Post-Production" textAlignment="CENTER" />
<Button fx:id="archiveButton" layoutX="714.0" layoutY="358.0" mnemonicParsing="false" onAction="#archiveButtonPushed" text="Archive" />
<Button fx:id="newJobButton" layoutX="29.0" layoutY="51.0" mnemonicParsing="false" onAction="#newJobButtonPressed" prefHeight="42.0" prefWidth="110.0" text="New Job" />
<Button fx:id="QuoteButton" layoutX="29.0" layoutY="121.0" mnemonicParsing="false" onAction="#QuoteButtonPushed" prefHeight="42.0" prefWidth="110.0" text="Quote" />
<Button fx:id="salesReportButton" layoutX="29.0" layoutY="316.0" mnemonicParsing="false" onAction="#salesReportButtonPushed" prefHeight="42.0" prefWidth="110.0" text="Sales Report" />
<Button fx:id="employeeButton" layoutX="29.0" layoutY="253.0" mnemonicParsing="false" onAction="#employeeButtonPushed" prefHeight="49.0" prefWidth="110.0" text="Employee" />
<Button fx:id="customerButton" layoutX="29.0" layoutY="184.0" mnemonicParsing="false" onAction="#customerButtonPushed" prefHeight="49.0" prefWidth="110.0" text="Customer" />
<Button fx:id="logoutButton" layoutX="716.0" layoutY="14.0" mnemonicParsing="false" onAction="#logoutButtonPushed" text="Logout" />
</children>
</AnchorPane>
Error
应用程序启动方法中的异常2018-12-03 13:18:02.370 INFO 19816 --- [lication Thread] j.LocalContainerEntityManagerFactoryBean:关闭持久性单元'default'的JPA EntityManagerFactory 2018-12-03 13:18:02.373 INFO 19816 --- [lication Thread] com.zaxxer.hikari.HikariDataSource:HikariPool-1 - Shutdown发起... 2018-12-03 13:18:02.379 INFO 19816 --- [lication Thread] com.zaxxer.hikari.HikariDataSource :HikariPool-1 - 关闭完成 . java.base中的java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)java.lang中的java.lang.reflect.InvocationTargetException(NativeMethodAccessorImpl.invoke:6 /jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.base / java.lang.reflect.Method.invoke(Method.java:564)at javafx.graphics/com.sun.javafx.application .LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473),位于java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0的javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)方法)java.base / java上java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.base / sun.launcher.LauncherHelper $ FXH中的.lang.reflect.Method.invoke(Method.java:564) elper.main(LauncherHelper.java:941)引起:java.lang.RuntimeException:javafx上的javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)的Application start方法中的异常 . java.base / java.lang.Thread.run(Thread.java:844)中的graphics / com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 2(LauncherImpl.java:198)引起:java.lang.NullPointerException在project_3368.project_3368.view.StageManager.prepareScene(StageManager.java:47)的project_3368.project_3368.view.StageManager.show(StageManager.java:30)at project_3368.project_3368.view.StageManager.switchScene(StageManager.java:26) )在project_3368.project_3368.Project3368Application.displayInitialScene(Project3368Application.java:41)at project_3368.project_3368.Project3368Application.start(Project3368Application.java:31)at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9 (LauncherImpl.java:919)在javafx.graphics/com.sun.javafx.applicati on.PlatformImpl.lambda $ runAndWait $ 11(PlatformImpl.java:449),位于java.base / java.security的javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418) . 位于javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)的AccessController.doPrivileged(Native Method)位于javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future . 在javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native方法)的javafx.graphics / com.sun.glass.ui.win.WinApplication.lambda $runLoop上运行(InvokeLaterDispatcher.java:96) $ 3(WinApplication.java:175)...另外1个异常运行应用程序project_3368.project_3368.Project3368Application