与颜色梯度的Borderpane

Pet*_*zov 4 javafx javafx-2 javafx-8

我想用绿色渐变色创建背景BorderPane,如下例所示.问题是如何使用Java代码执行此操作?

例如:

.linear-grad2{
   -fx-background-color: linear-gradient(from 25% 25% to 100% 100%, #dc143c, #32cd32);
}
Run Code Online (Sandbox Code Playgroud)

我想用颜色选择器改变颜色.我不知道如何用css代码做到这一点.

在此输入图像描述

Pat*_*ick 10

Hej Peter,

我不久前做过这样的事:

package de.professional_webworkx.blog.colorgradient;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @author ottp
 */
public class ColorGradient extends Application {

    @Override
    public void start(Stage primaryStage) {

        final Pane pane = new BorderPane();
        pane.setPrefWidth(300);
        pane.setPrefHeight(200);
        pane.setStyle("-fx-background-color: linear-gradient(from 25% 25% to 100% 100%, #dc143c, #661a33)");


        final ColorPicker picker = new ColorPicker();
        picker.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                Color value = picker.getValue();
                String colorString = value.toString();
                String substring = colorString.substring(2, colorString.length()-2);
                pane.setStyle("-fx-background-color: linear-gradient(from 25% 25% to 100% 100%, #" + substring + ", #661a33)");
            }
        });


        VBox vBox = new VBox();
        vBox.getChildren().add(pane);
        vBox.getChildren().add(picker);

        Scene scene = new Scene(vBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}
Run Code Online (Sandbox Code Playgroud)

将它作为您自己的解决方案的起点..它仅更改第一个颜色值.

帕特里克