2016-01-25 31 views
1

来自JavaFX CSS我想仅对提示文本应用效果,而不会影响TextField中的文本,但不知道如何访问该项目。我只能用-fx-prompt-text-fill来改变颜色。当我对文本应用效果时,提示文本也会受到影响,为什么?文本字段中的提示文本javaFX

.text-field { 
    -fx-prompt-text-fill: gray; 
} 
.text-field > .text { 
     -fx-effect: dropshadow(two-pass-box , blue , .5, 10 , 1 , 1);  
} 

在上面的代码也适用阴影提示文本我想避免的!

+0

你能解决您的CSS? '.textField'不是'TextField'的CSS类,你可以在CSS中嵌套选择器。 –

回答

2

仅当文本字段为空时,提示文本才会显示,因此我可以看到,执行此操作的最简单方法是定义并“清空”CSS PseudoClass。只要你想将它设置上的文字效果,然后再定义一个空的文本字段的文本效果为空:

.text-field { 
    -fx-prompt-text-fill: gray; 
} 

.text-field .text { 
    -fx-effect: dropshadow(two-pass-box , blue , .5, 10 , 1 , 1);  
} 

.text-field:empty .text { 
    -fx-effect: null ; 
} 

为了使伪类的工作,你需要注册与文本属性监听器在文本字段更新:

TextField textField = new TextField(); 
textField.setPromptText("Enter text"); 

PseudoClass empty = PseudoClass.getPseudoClass("empty"); 
textField.pseudoClassStateChanged(empty, true); 
textField.textProperty().addListener((obs, oldText, newText) -> { 
    textField.pseudoClassStateChanged(empty, newText.isEmpty()); 
}); 

这里是一个SSCCE(与CSS代码上面的提示文字 - styling.css):

import javafx.application.Application; 
import javafx.css.PseudoClass; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class TextFieldPromptStylingTest extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     TextField textField = new TextField(); 
     textField.setPromptText("Enter text"); 

     PseudoClass empty = PseudoClass.getPseudoClass("empty"); 
     textField.pseudoClassStateChanged(empty, true); 
     textField.textProperty().addListener((obs, oldText, newText) -> { 
      textField.pseudoClassStateChanged(empty, newText.isEmpty()); 
     }); 

     Button okButton = new Button("OK"); 
     VBox root = new VBox(10, textField, okButton); 
     root.setAlignment(Pos.CENTER); 
     root.setPadding(new Insets(24)); 

     Scene scene = new Scene(root); 
     scene.getStylesheets().add("prompt-text-styling.css"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

你可以风格,配有提示文本sytslesheet如果你se JFoenix。 CSS级是.jfx-text-field,属性-fx-prompt-text-fill

例子:

.jfx-text-field { 
-fx-prompt-text-fill: #989898; 
} 

如果需要其他组件,看它:JFoenix GitHub components page