格式化JTextField最多接受三位数字,但是最多可以输入1

编程入门 行业动态 更新时间:2024-10-23 17:33:24
本文介绍了格式化JTextField最多接受三位数字,但是最多可以输入1-3位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下JFormattedTextField

I have the following JFormattedTextField

try { MaskFormatter CabinNoTextF = new MaskFormatter("###"); CabinNoTextF.setPlaceholderCharacter(' '); CabinNoTextField = new JFormattedTextField(CabinNoTextF); centerPanelCabin.add(CabinNoTextField); addCabinF.add(centerPanelCabin); addCabinF.setVisible(true); } catch (Exception ex) { }

CabinNoTextField的格式设置为仅允许在文本字段中输入三个数字.但是,我想要它,以便用户也可以输入一个数字. IE.用户可以输入1,15或100.但是使用上面的代码,该文本字段仅允许输入三个数字,如果我在文本字段中输入一个数字,它将自动清除.

CabinNoTextField is formatted to only allow three digits to be inputted within the text field. However, I want it so that the user can also enter a single digit as well. I.e. the user may enter 1,15, or 100. But with the code above, the text field only allows three digits to be entered, if I enter a single digit within the text field, it automatically clears.

我如何允许CabinNoTextField最多接受三位数字,但文本字段也将接受一位数字和两位数.

How do I allow CabinNoTextField to accept three digits at most, but the text field will accept a single digit and double digits as well.

推荐答案

使用NumberFormat

NumberFormat amountFormat = NumberFormat.getNumberInstance(); amountFormat.setMinimumIntegerDigits(1); amountFormat.setMaximumIntegerDigits(3); amountFormat.setMaximumFractionDigits(0); amountField = new JFormattedTextField(amountFormat);

docs.oracle/javase/tutorial/uiswing/components/formattedtextfield.html

更多推荐

格式化JTextField最多接受三位数字,但是最多可以输入1

本文发布于:2023-11-10 06:19:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最多   数字   JTextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!