简体   繁体   中英

Javafx, css, setting a background image for a splitpane?

I am trying to set a picture as a background for a split pane. So the goal is, that no matter how I resize the two anchor panes by dragging the divider between them, I would like to have the background picture untouched.

I tried several things, but without success. I tried to add a css to the split pane in the Scene Builder:

.root {
 -fx-background-image: url("DSCF0806.JPG");
 }

This won't work... With a simple image view it wont work too, cause it makes itself a 3. splitted area, I can't put it under the two splitted area.

I couldn't find a technique for this until now... Do you have an idea? Thanks!

.root refers to the root of the Scene . Try the following in an external CSS file:

.split-pane {
 -fx-background-image: url("DSCF0806.JPG");
 }

For example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SplitPaneWithBackground extends Application {

    @Override
    public void start(Stage primaryStage) {
        SplitPane splitPane = new SplitPane();
        splitPane.getItems().addAll(new StackPane(new Label("Left")), 
                new StackPane(new Label("Right")));


        BorderPane root = new BorderPane(splitPane);
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add("split-pane-background.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

With the file split-pane-background.css:

.split-pane {
    /*
    The image used below is copyright of Fedaro (Fernando da Rosa) y Santiago Roland
    (http://commons.wikimedia.org/wiki/User:Fedaro) and is used under the terms of the
    Creative Commons Attribution-Share Alike Unported 3.0 license. 
    (http://creativecommons.org/licenses/by-sa/3.0/deed.en).
    */
    -fx-background-image: url("http://upload.wikimedia.org/wikipedia/commons/a/a7/Nebulosa_de_Eta_Carinae_o_NGC_3372.jpg");
    -fx-background-size: cover ;
}
.label {
    -fx-background-color: rgba(255, 255, 255, 0.5) ;
    -fx-padding: 10 ;
}

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