使用舞台管理器将CSS样式表添加到JavaFX / Spring应用程序

时间:2018-12-03 19:40:08

标签: java css spring javafx

我正在尝试将CS​​S样式表添加到我的应用程序中。我正在使用SceneBuilder,将样式表连接到SceneBuilder时,它显示得很好。但是,当我运行程序时,没有样式。我正在使用舞台管理员来切换舞台。我真的不确定在应用程序中将CSS加载到哪里。 CSS样式表已经连接到我的FXML文件中。当我尝试在我的舞台管理器中加载CSS时(使用scene.getStylesheets()。add(getClass()。getResource(“ ../../ java / MainCSS.css”)。toExternalForm());)- ,我得到了一个空指针异常。

项目布局:pic of layout 项目布局:pic of layout pt 2

阶段管理器

  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>

错误

  

应用程序启动方法中的异常2018-12-03 13:18:02.370 INFO   19816 --- [许可线程] j.LocalContainerEntityManagerManagerBean:   关闭持久性单元“默认”的JPA EntityManagerFactory   2018-12-03 13:18:02.373 INFO 19816 --- [许可线程]   com.zaxxer.hikari.HikariDataSource:HikariPool-1-关闭   发起... 2018-12-03 13:18:02.379信息19816 --- [许可线程]   com.zaxxer.hikari.HikariDataSource:HikariPool-1-关闭   完成。 java.lang.reflect.InvocationTargetException在   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:564)在   javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)     在   javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)     在   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:564)在   java.base / sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:941)   由以下原因引起:java.lang.RuntimeException:应用程序启动中的异常   方法   javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)     在   javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)     在java.base / java.lang.Thread.run(Thread.java:844)造成原因:   java.lang.NullPointerException在   project_3368.project_3368.view.StageManager.prepareScene(StageManager.java:47)在   project_3368.project_3368.view.StageManager.show(StageManager.java:30)     在   project_3368.project_3368.view.StageManager.switchScene(StageManager.java:26)在   project_3368.project_3368.Project3368Application.displayInitialScene(Project3368Application.java:41)     在   project_3368.project_3368.Project3368Application.start(Project3368Application.java:31)     在   javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)     在   javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)     在   javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)     在java.base / java.security.AccessController.doPrivileged(本机   方法)   javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)     在   javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)     在   javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机   方法)   javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)     ... 1个其他正在运行异常的应用程序   project_3368.project_3368.Project3368应用程序

0 个答案:

没有答案