我有一个实现keyListener的程序。我正在尝试做的是只允许按下数字、退格键、十进制键和返回箭头键。一切正常,除了当我按下返回箭头键时,它会删除我输入的数字'
public void keyReleased(KeyEvent e) {
try{
if(!Character.isDigit(e.getKeyChar()) && e.getKeyChar() != '.' && e.getKeyChar() != e.VK_BACK_SPACE && e.getKeyChar() != KeyEvent.VK_LEFT){
String input = inputIncome.getText();
inputIncome.setText(input.substring(input.length()-1));
}
}
catch(Exception arg){
}`
KeyListener
规则…
KeyListener
,即使这样,也要使用键绑定KeyListener
,而是使用DocumentFilter
基于您似乎正在更改JTextField
或文本组件的内容这一事实,您应该使用DocumentFilter
来过滤发送到字段Document
的更改,例如…
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
public class DocumentFilterExample {
public static void main(String[] args) {
new DocumentFilterExample();
}
public DocumentFilterExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField field = new JTextField(10);
((AbstractDocument)field.getDocument()).setDocumentFilter(new DecimalDocumentFilter());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(field);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DecimalDocumentFilter extends DocumentFilter {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr)
throws BadLocationException {
Document doc = fb.getDocument();
boolean hasDot = doc.getText(0, doc.getLength()).contains(".");
StringBuilder buffer = new StringBuilder(text);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch)) {
if ((ch == '.' && !hasDot)) {
hasDot = true;
} else {
buffer.deleteCharAt(i);
}
}
}
super.insertString(fb, offset, buffer.toString(), attr);
}
@Override
public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String string, AttributeSet attr) throws BadLocationException {
if (length > 0) {
fb.remove(offset, length);
}
insertString(fb, offset, string, attr);
}
}
}
这是基于这里的示例。
不以这种方式使用KeyListener
的原因有很多,例如…
Document
只是举几个例子