我正在 javafx 中制作场景切换程序,但是当我单击切换时它显示错误,但是当我在没有 css 的情况下执行它时它工作正常

I am making a scene switch program in javafx but when i click to switch it shows error, but when i do it without css it worked fine

这是我的 App.java 文件

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;

public class App extends Application {
    @Override
    public void start(Stage stage) {
       try {
           Parent root = FXMLLoader.load(getClass().getResource("App.fxml"));
           Scene scene = new Scene(root);
           String css= this.getClass().getResource("application.css").toExternalForm();
           scene.getStylesheets().add(css);
           stage.setScene(scene);
           stage.show();
       } catch (Exception e) {
           e.printStackTrace();
      }
  }
   public static void main(String[] args) throws Exception {
       Application.launch(args);
  }
 }

这是我的 SwitchController.java 文件

import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class SceneController {

private Stage stage;
private Scene scene;
private Parent parent;

public void switchToApp(ActionEvent event) throws IOException {
    parent = FXMLLoader.load(getClass().getResource("App.fxml"));
    stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    String css = this.getClass().getResource("application.css").toExternalForm();
    scene.getStylesheets().add(css);
    scene = new Scene(parent);
    stage.setScene(scene);
    stage.show();
}

public void switchToApp2(ActionEvent event) throws IOException {
    parent = FXMLLoader.load(getClass().getResource("App2.fxml"));
    stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    String css = this.getClass().getResource("application.css").toExternalForm();
    scene.getStylesheets().add(css);
    scene = new Scene(parent);
    stage.setScene(scene);
    stage.show();
 }
}

这是我的 application.css 文件

.root{
    -fx-background-color: "red";
    
}
#Sc1{
    -fx-alignment: center;
}
#Sc2{
    -fx-alignment: center;
    -fx-text-fill: "GREEN";
    -fx-outline:none;
}

.label{
    -fx-font-size: 20;
    -fx-font-family: "Lucida Console";
    -fx-text-fill: "white";
    -fx-alignment: center;
}

#titleLebel{
    -fx-font-size: 60;
}

这是我的 App2.fxml 文件

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneController">
<children>
<Button fx:id="Sc1" layoutX="197.0" layoutY="272.0" mnemonicParsing="false" onAction="#switchToApp" prefHeight="84.0" prefWidth="187.0" text="Switch to Scene 1" />
  <Label layoutX="1.0" layoutY="50.0" prefHeight="17.0" prefWidth="600.0" text="This is the Scene 2" />
</children>
</AnchorPane>

这是我的 App.fxml 文件

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneController">
  <children>
  <Label fx:id="titleLebel" layoutX="1.0" layoutY="143.0" prefHeight="17.0" prefWidth="600.0" text="Level 1" />
  <Label fx:id="secondlevewl" layoutX="2.0" layoutY="242.0" prefHeight="17.0" prefWidth="600.0" text="this is some css styling" />
  <Button fx:id="Sc2" layoutX="213.0" layoutY="292.0" mnemonicParsing="false" onAction="#switchToApp2" prefHeight="78.0" prefWidth="177.0" text="Switch to Scene 2" />
</children>
</AnchorPane>

I am just a newbie to learning JavaFX and Java from [Bro code]https://www.youtube.com/watch?v=9XJicRt_FaI&ab_channel=BroCode do you guys have any suggestions please comment thank you

正如@n247s 和@kleopatra 所建议的,您需要提供您遇到的错误并有更好的命名约定,以便读者可以轻松理解逻辑。

话虽如此,我很容易注意到的一件事是您分配样式表的方式。首先,您将样式表分配给场景,然后创建或启动它,这会导致 NullPointerExecption 或样式表对场景没有影响。您需要修改该行以在场景创建后分配它。

类似于下面的代码:

public class SceneController {

    private Stage stage;
    private Scene scene;
    private Parent parent;

    public void switchToApp(ActionEvent event) throws IOException {
        parent = FXMLLoader.load(getClass().getResource("App.fxml"));
        addScene(event);
    }

    public void switchToApp2(ActionEvent event) throws IOException {
        parent = FXMLLoader.load(getClass().getResource("App2.fxml"));
        addScene(event);
    }
     
     private void addScene(ActionEvent event){
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(parent);
        String css = this.getClass().getResource("application.css").toExternalForm();
        // Set the stylesheet after the scene creation
        scene.getStylesheets().add(css);
        stage.setScene(scene);
        stage.show();
     }
}