java中的jtextfield只允许数字和一个小数点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18102740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only numbers and one decimal point allow on jtextfield in java
提问by Gayan Weerakkodi
Have a
jtextfield
in my java form. I want to not allow to type alpha(Characters) and only allow to type numbers.And can't type 2 decimal point and two numbers after the decimal point.(that
jtextfield
for price..)
jtextfield
在我的 java 表单中有一个。我不想允许输入 alpha(Characters) 而只允许输入数字。并且不能输入2个小数点和小数点后的两个数字。(
jtextfield
价格..)
Please tell me how to do this step by step.
请告诉我如何一步一步地做到这一点。
回答by Nidhish Krishnan
Try this code if you want only number to be inputed in you textfield
如果您只想输入数字,请尝试使用此代码 textfield
txtfield.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= 0) && (c <= 9) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
e.consume();
}
}
});
paste the above code in your constructor after txtfield
initialization
txtfield
初始化后将上述代码粘贴到您的构造函数中
also take a look at this stuff JFormattedTextField for Double still takes characters
回答by StanislavL
Use JFormattedTextField
with NumberFormat (See for example). Alternatively you can add your own DocumentFilter
to the document of your JTextField
JFormattedTextField
与 NumberFormat 一起使用(参见示例)。或者,您可以将自己的添加到您DocumentFilter
的文档中JTextField
回答by mKorbel
Please tell me how to do this step by step.
请告诉我如何一步一步地做到这一点。
use JFormattedTextField with NumberFormatter
restrict number of decimal places
set various RoundingModes
restrict range, set minimal and/or maximal value
another of ways is to use JSpinner with SPinnerNUmberModel, but required to use DocumentFilter
将 JFormattedTextField 与 NumberFormatter 一起使用
限制小数位数
设置各种 RoundingModes
限制范围,设置最小值和/或最大值
另一种方法是将 JSpinner 与 SPinnerNUMberModel 一起使用,但需要使用 DocumentFilter
for example
例如
import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public DocumentListenerAdapter() {
JFrame frame = new JFrame("AbstractTextField Test");
final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
textField1.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
textField2.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
textField2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) textField2.getValue()).doubleValue());
if (t1a1 > 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font(attributes));
textField2.setForeground(Color.red);
}
};
SwingUtilities.invokeLater(doRun);
} else {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font("Serif", Font.BOLD, 16));
textField2.setForeground(Color.black);
}
};
SwingUtilities.invokeLater(doRun);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DocumentListenerAdapter main = new DocumentListenerAdapter();
}
});
}
}