?? 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 + -