我尝试使用显示here的示例,但java显示
的错误消息“无法将AttributeSet解析为类型”
这就是为什么我试图使用另一种只允许数字的方法:
txtUsername.addKeyListener(new MyKeyListener());
public class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent ke){
System.out.println("Key pressed code = "+ke.getKeyCode());
if (ke.getKeyCode()>=48 && ke.getKeyCode()<=57)
return true;
else
return false;
}
}
但当然它不起作用,因为keyPressed
方法是void
。那么,为了在textfield中只打印数字怎么办?
答案 0 :(得分:25)
在这里查看这段代码片段,通过使用DocumentFilter,只允许JTextField中的数字作为最有效的方式:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
public class InputInteger
{
private JTextField tField;
private MyDocumentFilter documentFilter;
private void displayGUI()
{
JFrame frame = new JFrame("Input Integer Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
tField = new JTextField(10);
((AbstractDocument)tField.getDocument()).setDocumentFilter(
new MyDocumentFilter());
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new InputInteger().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class MyDocumentFilter extends DocumentFilter
{
@Override
public void insertString(DocumentFilter.FilterBypass fp
, int offset, String string, AttributeSet aset)
throws BadLocationException
{
int len = string.length();
boolean isValidInteger = true;
for (int i = 0; i < len; i++)
{
if (!Character.isDigit(string.charAt(i)))
{
isValidInteger = false;
break;
}
}
if (isValidInteger)
super.insertString(fp, offset, string, aset);
else
Toolkit.getDefaultToolkit().beep();
}
@Override
public void replace(DocumentFilter.FilterBypass fp, int offset
, int length, String string, AttributeSet aset)
throws BadLocationException
{
int len = string.length();
boolean isValidInteger = true;
for (int i = 0; i < len; i++)
{
if (!Character.isDigit(string.charAt(i)))
{
isValidInteger = false;
break;
}
}
if (isValidInteger)
super.replace(fp, offset, length, string, aset);
else
Toolkit.getDefaultToolkit().beep();
}
}
@Override
public void insertString(FilterBypass fb, int off
, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.insertString(off, str.replaceAll("\\D++", ""), attr);
}
@Override
public void replace(FilterBypass fb, int off
, int len, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.replace(off, len, str.replaceAll("\\D++", ""), attr);
}
答案 1 :(得分:9)
答案 2 :(得分:0)
试试这个:
public void replace(DocumentFilter.FilterBypass fb, int offset,
int length, String text, AttributeSet attrs)throws BadLocationException {
if(fb.getDocument().getLength() + text.length() > 10) {
return;
}
fb.insertString(offset, text, attrs);
}
而不是:
public void replace(DocumentFilter.FilterBypass fp, int offset
, int length, String string, AttributeSet aset)
throws BadLocationException
{
int len = string.length();
boolean isValidInteger = true;
for (int i = 0; i < len; i++)
{
if (!Character.isDigit(string.charAt(i)))
{
isValidInteger = false;
break;
}
}
if (isValidInteger)
super.replace(fp, offset, length, string, aset);
else
Toolkit.getDefaultToolkit().beep();
}
答案 3 :(得分:0)
这就是我用来消耗非数字
的方法textField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
e.consume(); // consume non-numbers
}
}
});
答案 4 :(得分:0)
我的版本完全拒绝不需要的字符(我尝试过):
val2 = new JTextField();
val2.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
try {
char input = e.getKeyChar(); //checks each key pressed
// in other words it prevents input of any character other than 0-9 or .
try {
int i = Integer.parseInt(val2.getText());
valid1.setText(""); //valid1 is a label for the message
} catch (Exception e2) {
// TODO: handle exception
valid1.setText("Invalid Number!");
}
if((!Character.isDigit(input)) && !(input == '.')) { //numbers accept "."
e.consume();
// event won't be sent for any further event listeners
try {
int i = Integer.parseInt(val2.getText()); // checks if the string can be converted to int
double i1 = Double.parseDouble(val2.getText()); // checks if string can be converted to double
valid1.setText("");
} catch (Exception e2) {
// TODO: handle exception
// if it is not in a valid format.. it will generate error message
valid1.setText("Invalid Number!");
}
}
else {
// if format and characters are fine... no output
valid1.setText("");
}
}
catch (Exception e2) {
// TODO: handle exception
// for unexpected errors
valid1.setText("Error");
}
}
});
// auto generated for design of the textfield
val2.setColumns(10);
val2.setBounds(236, 11, 126, 43);
panel.add(val2);