在JavaFX中建立数字TextField的推荐方法是什么? [英] What is the recommended way to make a numeric TextField in JavaFX?

查看:352
本文介绍了在JavaFX中建立数字TextField的推荐方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将输入限制为一个TextField为整数。任何建议吗?

I need to restrict input into a TextField to integers. Any advice?

推荐答案

非常旧的线程,但看起来更整洁,如果贴上去掉非数字字符。

Very old thread, but this seems neater and strips out non-numeric characters if pasted.

// force the field to be numeric only
textField.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, 
        String newValue) {
        if (!newValue.matches("\\d*")) {
            textField.setText(newValue.replaceAll("[^\\d]", ""));
        }
    }
});

这篇关于在JavaFX中建立数字TextField的推荐方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆