在我的java表单中有一个jtextfield
。我想不允许输入字母(字符),只允许输入数字。
并且不能在小数点后输入2个小数点和两个数字。(jtextfield
表示价格..)
请告诉我如何逐步完成此操作。
答案 0 :(得分:3)
将JFormattedTextField
与NumberFormat一起使用(请参阅for example)。或者,您可以将自己的DocumentFilter
添加到JTextField
答案 1 :(得分:3)
请告诉我如何逐步完成此操作。
将JFormattedTextField与NumberFormatter一起使用
限制小数位数
设置各种RoundingModes
限制范围,设置最小值和/或最大值
另一种方法是将JSpinner与SPinnerNUmberModel一起使用,但需要使用DocumentFilter
例如
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();
}
});
}
}
答案 2 :(得分:2)
如果您只希望在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();
}
}
});
在txtfield
初始化
还要看看这些内容 JFormattedTextField for Double still takes characters