java textfield 数字,只接受Java TextField中的数字和点

I've got one textField where I only accept numbers from the keyboard, but now I have to change it as it's a "price textField" and I would also need to accept a dot "." for any kind of prices.

How can I change this in order to get what I need?

ptoMinimoField = new JTextField();

ptoMinimoField.setBounds(348, 177, 167, 20);

contentPanel.add(ptoMinimoField);

ptoMinimoField.setColumns(10);

ptoMinimoField.addKeyListener(new KeyAdapter() {

public void keyTyped(KeyEvent e) {

char caracter = e.getKeyChar();

if (((caracter < '0') || (caracter > '9'))

&& (caracter != '\b')) {

e.consume();

}

}

});

解决方案

Formatted text fields provide a way for developers to specify the valid set of characters that can be typed in a text field.

amountFormat = NumberFormat.getNumberInstance();

...

amountField = new JFormattedTextField(amountFormat);

amountField.setValue(new Double(amount));

amountField.setColumns(10);

amountField.addPropertyChangeListener("value", this);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值