📄 jmoneyfield.java
字号:
import javax.swing.*;
import javax.swing.text.*;
/**
* 数字/金钱输入框类,继承 JTextField 类
* @author XiaoweiHong
* */
public class JMoneyField extends JTextField {
/*public void JMoneyField(){
new JTextField();
}*/
class MoneyForMatDocument extends PlainDocument {
int maxLen=Integer.MAX_VALUE; /*[输入框最大长度]*/
int Digitale=100; /*[输入框小数位最大长度]*/
int thisLen=0; /*[当前输入框字符串长度]*/
/**
* 构造MeneyForMatDocument类
* @param maxLength 输入框最大长度
* @param Dig 输入框小数位最大长度
* */
public MoneyForMatDocument(int maxLength,int Dig)
{
this.maxLen=maxLength;
this.Digitale=Dig;
}
/**
* 输入字符响应事件
* @param offs 起始偏移量,该值 >= 0
* @param str 要插入的字符串;null/空字符串不执行任何操作
* @param a 插入内容的属性
* */
public void insertString(int offs,
String str,
AttributeSet a)
throws BadLocationException{
if(str==null||thisLen>=maxLen)
return;
char ch=str.charAt(0);
thisLen=this.getContent().length();
String str_all=this.getContent().getString(0,thisLen);
if(ch>='0'&&ch<='9'){
if(str_all.indexOf('.')!=-1&&(thisLen-str_all.indexOf('.')-1>Digitale)){
return;
}
thisLen++;
super.insertString(offs, str, a);
return;
}
if(str.equals(".")){
if(str_all.indexOf('.')==-1){
thisLen++;
super.insertString(offs, str, a);
}
}
}
/**
* 移除字符响应事件
* @param offs 起始偏移量,该值 >= 0
* @param len 移除字符串长度
* */
public void remove(int offs,int len)
throws BadLocationException{
thisLen-=len;
super.remove(offs, len);
}
}
/**
* 构造 数字/金钱 输入框类
* @param Text 默认字符串
* @param Maxlen 输入框最大长度
* @param Digital 输入框小数位最大长度
* */
public JMoneyField(String Text,int Maxlen,int Digital){
new JTextField(Text,Maxlen);
this.setHorizontalAlignment(JMoneyField.RIGHT);
this.setDocument(new MoneyForMatDocument(Maxlen,Digital));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -