简体   繁体   中英

JavaFX can't find CSS Class after switching windows

I am trying to make a program using JavaFX, unfortunately I have this problem that has been driving me crazy these past hours.

I have created a small program to describe it to you.

The program starts with the Main.java : it boots up the UI.

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            VBox root = (VBox)FXMLLoader.load(getClass().getResource("sanic.fxml"));
            Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("app.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("sanic");
        primaryStage.show();
        primaryStage.sizeToScene();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

    public static void main(String[] args) {
        launch(args);
    }
}

On the UI, there is just a simple button which leads us to another page:

public class SanicController {

    @FXML
    public void goesToKnakles(ActionEvent event) {
        try {
            HBox root = (HBox)FXMLLoader.load(getClass().getResource("knakles.fxml"));
            Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
            Scene s = new Scene(root);
            s.getStylesheets().add(getClass().getResource("app.css").toExternalForm());
            stage.setScene(s);
            stage.show();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Simple enough so far. On this new page, I have this controller:

public class KnaklesController {

    @FXML
    private HBox box;

    public void initialize() {
        try {
            Label l = new Label("ur my best friend");
            l.getStylesheets().addAll("test");
            this.box.getChildren().add(l);
        }
    
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

I just have an HBox and I want to add a Label to it. The Label gets added, however when I want to apply a style to it, Eclipse tells me the following error:

Dec 27, 2017 12:01:48 AM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "test" not found.

And finally, here is my CSS:

HBox{
    -fx-background-color: #FF00FF;
}

#test {
    -fx-background-color: #FFFFFF;
}

Would anyone know WHY the program can't find the appropriate CSS class? This makes me very confused as I have actually successfully applied CSS classes on other parts of my actual program...

If anyone could take the time to solve this I would be extremely grateful.

Alright nevermind I am a goddamn dumbass, it is

 getStyleClass()

and not

 getStylesheets()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM