在jTextField中显示默认文本



我想要一个默认的"$(美元符号(";在我的文本字段中,即使有另一个输出要显示,即使我需要在同一文本字段中输入一些内容,它也不应该消失。

没关系,我自己解决了,呵呵:DDDDD

如果要向文本字段显示内容,只需在要显示的内容的开头添加$。

如果你想让它总是在输入上,你可以尝试这样的东西:

textBox.setText("$ ");
textBox.textProperty().addListener((observable, oldValue, newValue) -> {
try {
if(textBox.getText().charAt(0) == "$".charAt(0) && textBox.getText().charAt(1) == " ".charAt(0)) {
// there is a $ at the beginning
} else {
// there is no $ and we have to insert it
textBox.setText("$ " + textBox.getText());
}
}
catch (Exception e) {
textBox.setText("$ ");
}
});

最新更新