[英]Adding $ sign into the textfield when the user enter in the field
当用户在字段中输入$并禁止用户取消$符号时,任何人都知道如何自动将$符号添加到文本字段中。
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 $
问题是用户可能会删除
$
符号,并且验证非常严格,这意味着输入字段中的文本必须以$
开头
Another possibility is to use the BuddySupport
API from Swing Labs, SwingX library 另一种可能性是使用Swing Labs的SwingX库中的
BuddySupport
API
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) 这意味着
$
是与实际字段分开的组成部分,用户无法删除(但包含在字段中,因此不受父容器的布局管理器的影响)
The java.awt.TextField could be monitored for changes by adding a TextListener for TextEvent's.
通过为TextEvent添加TextListener,可以监视java.awt.TextField的更改。 In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners.
在基于JTextComponent的组件中,更改从模型通过DocumentEvent广播到DocumentListeners。 The DocumentEvent gives the location of the change and the kind of change if desired.
DocumentEvent提供更改的位置和更改的种类(如果需要)。
You need to use the DocumentListener
and combine it with some Regex
to do the magic. 您需要使用
DocumentListener
并将其与某些Regex
结合使用以完成魔术。 If the text at a given point does not match the format you want, do not update the JTextField
or simply using the .charAt()
method would do but then it is up to you 如果给定点的文本与所需的格式不匹配,请不要更新
JTextField
或仅使用.charAt()
方法即可,但由您自己决定
SSCCE without DocumentListener 没有DocumentListener的 SSCCE
package stack;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class OhMyDollar {
static JFrame frame;
static JTextField field;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
frame = new JFrame("Useless Title");
field = new JTextField("$", 30);
frame.getContentPane().add(field);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
field.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent event) {
}
@Override
public void keyReleased(KeyEvent event) {
}
@Override
public void keyTyped(KeyEvent event) {
StringBuffer text = new StringBuffer(field.getText());
StringBuffer dollar = new StringBuffer("$");
if(field.getText().isEmpty() || text.charAt(0)!='$'){
field.setText(dollar.append(text).toString());
}
}
});
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.