在消息应用中,当您键入时,角落上的计数器标签会更新,显示输入的字符数。如何在 Java 中创建相同的东西?
最佳答案
从 TextField
的 text
属性中检索包含其长度的 IntegerBinding
并将其转换为 StringBinding
以适合您需求的方式。将输出 Label
的 text
属性绑定(bind)到结果:
@Override
public void start(Stage primaryStage) {
TextField textField = new TextField();
Label label = new Label();
label.textProperty().bind(textField.textProperty()
.length()
.asString("Character Count: %d"));
VBox root = new VBox(label, textField);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
关于java - 如何在java中制作实时消息或字符计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161005/