当前位置: 技术问答>java相关
有之中数字输入的TextField的TextField的类代码吗?
来源: 互联网 发布时间:2015-11-12
本文导语: 有之中数字输入的TextField的TextField的类代码吗? | JTextField text= new JTextField(3); text.setDocument(new NumberDocument(text, 4)); text.setHorizontalAlignment(JTextField.RIGHT); NumberDocument.java import java.awt.Toolkit; impo...
有之中数字输入的TextField的TextField的类代码吗?
|
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()
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()
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!