2014-07-11 28 views

Trả lời

9

Kiểu CSS cho điều khiển nhập văn bản như TextField cho JavaFX 8 được xác định trong biểu định kiểu modena.css như bên dưới. Tạo biểu định kiểu CSS tùy chỉnh và sửa đổi màu như bạn muốn. Sử dụng CSS reference guide nếu bạn cần trợ giúp để hiểu cú pháp và các thuộc tính và giá trị có sẵn.

.text-input { 
    -fx-text-fill: -fx-text-inner-color; 
    -fx-highlight-fill: derive(-fx-control-inner-background,-20%); 
    -fx-highlight-text-fill: -fx-text-inner-color; 
    -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%); 
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border), 
     linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background); 
    -fx-background-insets: 0, 1; 
    -fx-background-radius: 3, 2; 
    -fx-cursor: text; 
    -fx-padding: 0.333333em 0.583em 0.333333em 0.583em; /* 4 7 4 7 */ 
} 
.text-input:focused { 
    -fx-highlight-fill: -fx-accent; 
    -fx-highlight-text-fill: white; 
    -fx-background-color: 
     -fx-focus-color, 
     -fx-control-inner-background, 
     -fx-faint-focus-color, 
     linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background); 
    -fx-background-insets: -0.2, 1, -1.4, 3; 
    -fx-background-radius: 3, 2, 4, 0; 
    -fx-prompt-text-fill: transparent; 
} 

Mặc dù sử dụng một kiểu dáng bên ngoài là một cách ưa thích để làm phong cách, bạn có thể tạo kiểu inline, sử dụng một cái gì đó như dưới đây:

textField.setStyle("-fx-text-inner-color: red;"); 
+0

Tôi có đối tượng màu. Làm thế nào tôi có thể thay đổi màu textfield bằng cách sử dụng này? – alhelal

+1

@alhelal chuyển đổi đối tượng màu thành chuỗi bằng cách sử dụng phương pháp ở đây: [Cách lấy chuỗi web hex từ màu JavaFX ColorPicker?] (Https://stackoverflow.com/questions/17925318/how-to-get-hex-web -string-from-javafx-colorpicker-color), sau đó thiết lập màu cho chuỗi mà bạn bắt nguồn: 'textField.setStyle (" - fx-text-inner-color: "+ colorString +"; ");' – jewelsea

19

Thiết lập -fx-text-fill công trình đối với tôi.

Xem dưới đây:

if (passed) { 
    resultInfo.setText("Passed!"); 
    resultInfo.setStyle("-fx-text-fill: green; -fx-font-size: 16;"); 
} else { 
    resultInfo.setText("Failed!"); 
    resultInfo.setStyle("-fx-text-fill: red; -fx-font-size: 16;"); 
} 
Các vấn đề liên quan