JTextField text= new JTextField(3);
text.setDocument(new NumberDocument(text, 4));
text.setHorizontalAlignment(JTextField.RIGHT);NumberDocument.java
import java.awt.Toolkit;
import javax.swing.text.*;/**
* This class provide a documnent that only number can input
* and can set max length of the document. It extends
* javax.swing.text.PlainDocument.
*
* @version 1.0 5-Sep-2002
*/
public class NumberDocument extends PlainDocument {
private JTextComponent tc;
private int maxLength; /**
* This constructor create a NumberDocument use the JTextComponent
* that use this document and the max length of this document.
*
* @param JTextComponent tc The JTextComponent use this document.
* @param int maxLength The max length of this document.
*/
public NumberDocument(JTextComponent tc, int maxLength) {
this.tc = tc;
this.maxLength = maxLength;
} /**
* Inserts some content into the document. Override the method of
* PlainDocument, only allow the number input and ingore the input
* after max length.
*
* @param offs the starting offset >= 0
* @param str the string to insert; does nothing with null/empty strings
* @param a the attributes for the inserted content
* @exception BadLocationException the given insert position is not a valid
* position within the document
*/
public void insertString(int offset, String s, AttributeSet attributeSet)
throws BadLocationException {
try {
Integer.parseInt(s);
} catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
return;
}
if (tc.getText().length() < maxLength) {
super.insertString(offset, s, attributeSet);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
text.setDocument(new NumberDocument(text, 4));
text.setHorizontalAlignment(JTextField.RIGHT);NumberDocument.java
import java.awt.Toolkit;
import javax.swing.text.*;/**
* This class provide a documnent that only number can input
* and can set max length of the document. It extends
* javax.swing.text.PlainDocument.
*
* @version 1.0 5-Sep-2002
*/
public class NumberDocument extends PlainDocument {
private JTextComponent tc;
private int maxLength; /**
* This constructor create a NumberDocument use the JTextComponent
* that use this document and the max length of this document.
*
* @param JTextComponent tc The JTextComponent use this document.
* @param int maxLength The max length of this document.
*/
public NumberDocument(JTextComponent tc, int maxLength) {
this.tc = tc;
this.maxLength = maxLength;
} /**
* Inserts some content into the document. Override the method of
* PlainDocument, only allow the number input and ingore the input
* after max length.
*
* @param offs the starting offset >= 0
* @param str the string to insert; does nothing with null/empty strings
* @param a the attributes for the inserted content
* @exception BadLocationException the given insert position is not a valid
* position within the document
*/
public void insertString(int offset, String s, AttributeSet attributeSet)
throws BadLocationException {
try {
Integer.parseInt(s);
} catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
return;
}
if (tc.getText().length() < maxLength) {
super.insertString(offset, s, attributeSet);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
Integer.parseInt(s);
} catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
return;
}
改为
try {
Double.parseDouble(s);
} catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
return;
}