📄 customtextfield.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
/** This class extends JTextField to provide the options of only alphabetic, numeric data.
*/
public class CustomTextField extends JTextField {
private int type;
public CustomTextField(int cols) {
super(cols);
}
public CustomTextField(int cols,int type){
super(cols);
this.type=type;
}
public CustomTextField(){
type=Utilities.ALPHABETIC;
}
protected Document createDefaultModel() {
return new DiscardChars(type);
}
}
class DiscardChars extends PlainDocument {
private int type;
DiscardChars(int type){
this.type=type;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
char[] chars = str.toCharArray();
chars=selectChars(chars);
super.insertString(offs, new String(chars), a);
}
private char[] selectChars(char [] ch){
int j=0,i=0;
String temp="";
if(type==0)
for(;i<ch.length;i++)
if((ch[i]>=65&&ch[i]<=90)||(ch[i]>=97&&ch[i]<=122)||ch[i]==8||ch[i]==32)
temp=temp+ch[i];
else if(type==1)
for(;i<ch.length;i++)
if(ch[i]>=48&&ch[i]<=57||ch[i]==8||ch[i]==32)
temp=temp+ch[i];
else
temp=new String(ch);
return temp.toCharArray();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -