2014-02-25 182 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