Issue with background color in JavaFX 8

后端 未结 3 1575
北海茫月
北海茫月 2021-02-12 13:40

Looks like there is an issue with setting background colors for panels in JavaFX 8.

I had been trying the below, but none of them set the appropriate background colors.<

3条回答
  •  太阳男子
    2021-02-12 14:24

    Both these work for me. Maybe post a complete example?

    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.ToggleButton;
    import javafx.scene.layout.Background;
    import javafx.scene.layout.BackgroundFill;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.CornerRadii;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class PaneBackgroundTest extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            BorderPane root = new BorderPane();
            VBox vbox = new VBox();
            root.setCenter(vbox);
    
            ToggleButton toggle = new ToggleButton("Toggle color");
            HBox controls = new HBox(5, toggle);
            controls.setAlignment(Pos.CENTER);
            root.setBottom(controls);
    
    //        vbox.styleProperty().bind(Bindings.when(toggle.selectedProperty())
    //                .then("-fx-background-color: cornflowerblue;")
    //                .otherwise("-fx-background-color: white;"));
    
            vbox.backgroundProperty().bind(Bindings.when(toggle.selectedProperty())
                    .then(new Background(new BackgroundFill(Color.CORNFLOWERBLUE, CornerRadii.EMPTY, Insets.EMPTY)))
                    .otherwise(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY))));
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

提交回复
热议问题