当用户在字段中输入时将 $ 符号添加到文本字段中 [英] Adding $ sign into the textfield when the user enter in the field

查看:34
本文介绍了当用户在字段中输入时将 $ 符号添加到文本字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何在用户输入字段时自动将 $ 符号添加到文本字段中,并禁止用户取消 $ 符号.

Anyone know how to auto add $ sign into the textfield when the user enter in the field and also disable the user from cancel the $ sign.

推荐答案

不是严格"你在问什么,而是从 如何用于出席文本字段,例如...

Not "strictly" what you're asking, but start by taking How to use for attend text fields, for example...

paymentField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
paymentField.setValue(new Double(payment));
paymentField.setColumns(10);
paymentField.setEditable(false);

问题在于用户可以删除 $ 符号并且验证非常严格,这意味着输入到字段中的文本必须以 $

The problem with this is it's possible for the user to remove the $ sign and the validation is very strict, meaning that the text entered into the field MUST start with a $

另一种可能性是使用来自 Swing Labs、SwingX 库的 BuddySupport API

Another possibility is to use the BuddySupport API from Swing Labs, SwingX library

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits(2);
paymentField = new JFormattedTextField(nf);
paymentField.setValue(100d);
paymentField.setColumns(10);
paymentField.setEditable(false);
BuddySupport.addLeft(new JLabel("$"), paymentField);

这意味着 $ 是一个独立于实际字段的组件,不能被用户删除(但包含在字段中,所以它不受父容器的布局管理器的影响))

This means that that $ is a separate component from the actual field and can't be removed by the user (but is contained within the field so it is not affected by the parent container's layout manager)

这篇关于当用户在字段中输入时将 $ 符号添加到文本字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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