2014-02-25 165 views
1

我想用下面的这个例子创建带有绿色渐变颜色的背景BorderPane。问题是我如何用Java代码来做到这一点?带颜色渐变的Borderpane

例如:

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

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

enter image description here

回答

5

HEJ彼得,

我这样做是前一段时间是这样的:

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); 
    } 

} 

把它作为一个起点,自己的解决方案.. 它只改变第一颜色值。

Patrick