2017-08-10 60 views
1

我正在構建一個涉及JavaFX中的TextAreaTextField的應用程序。我想包括能夠更改ColorPicker的字體顏色。我可以通過以下操作很容易地自定義背景顏色:ColorPicker中的TextField中的字體顏色

backgroundColorPicker.setOnAction(event -> { 
    Color color = backgroundColorPicker.getValue(); 
    Background background = new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)); 
    Region region = (Region) console.lookup(".content"); 
    region.setBackground(background); 
    input.setBackground(background); 
}); 

如何更改字體顏色?到目前爲止,我只有

foregroundColorPicker.setOnAction(event -> { 
    Color color = foregroundColorPicker.getValue();   
}); 

我一直無法找到一種方法來更改字段上的字體顏色。

+0

'setBackgroundColor'的insteed使用'setForegroundColor' – Antoniossss

+0

@Antoniossss我不相信我使用'setBackgroundColor',是在不同的圖書館嗎? – JoseRivas1998

+1

使用CSS;文本字段具有「text-fill」屬性。你可以根據顏色創建合適的字符串,只需調用'setStyle(...)'選擇一個「quick and dirty」選項,或者使用外部CSS文件並使用查找的顏色...... –

回答

1

我能夠通過使用顏色值並將其轉換爲CSS,然後將該CSS應用於字段來解決此問題。

foregroundColorPicker.setOnAction(event -> { 
    Color color = foregroundColorPicker.getValue(); 
    double red = color.getRed() * 255; 
    double green = color.getGreen() * 255; 
    double blue = color.getBlue() * 255; 
    double alpha = color.getOpacity() * 255; 
    String colorString = String.format("-fx-text-fill: rgba(%f,%f,%f,%f) ;", red, green, blue, alpha); 
    console.setStyle(colorString) ; 
    input.setStyle(colorString); 
});