JTextfield 的输入限制问题 1、判断asc码的取值范围不就行了2、键盘监听,可以实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 想要监听键盘输入,需要安装文本过滤器,覆盖格式器类的方法,并且传递格式器类的对象到JFormattedTextField.试试这个:import java.awt.*;import java.awt.event.*;import java.text.*;import javax.swing.*;import javax.swing.text.*;public class TestInputNumber { public static void main(String[] args) { FormatTestFrame frame = new FormatTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }}class FormatTestFrame extends JFrame { public FormatTestFrame() { setTitle("FormatTest"); setSize(300, 100); Container contentPane = getContentPane(); JLabel lblIntField = new JLabel("NumberFilter:"); JFormattedTextField intField = new JFormattedTextField(new InternationalFormatter( NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return filter; } private DocumentFilter filter = new IntFilter(); }); intField.setValue(new Integer(100)); intField.setColumns(10); JPanel mainPanel = new JPanel(); mainPanel.add(lblIntField); mainPanel.add(intField); contentPane.add(mainPanel, BorderLayout.CENTER); } class IntFilter extends DocumentFilter { public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { StringBuffer buffer = new StringBuffer(string); for (int i = buffer.length() - 1; i >= 0; i--) { char ch = buffer.charAt(i); if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER) buffer.deleteCharAt(i); } super.insertString(fb, offset, buffer.toString(), attr); } public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException { if (string != null) { StringBuffer buffer = new StringBuffer(string); for (int i = buffer.length() - 1; i >= 0; i--) { char ch = buffer.charAt(i); if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER) buffer.deleteCharAt(i); } string = buffer.toString(); } super.replace(fb, offset, length, string, attr); } }} 下面是只能输入数字和"\"的JTextField,另外还增加了输入最多字符个数的限制:import javax.swing.*;import javax.swing.text.*;import java.awt.*;public class test extends JTextField{ private numDoc doc = null; public test() { super(12); setDocument( doc = new numDoc()); } public test(int maxLen) { super(12); setDocument( doc = new numDoc(maxLen)); } public void setMaxLen( int maxLen ) { doc.setMaxLen( maxLen ); } public int getMaxLen() { return doc.getMaxLen(); } public static void main( String args[] ) { JFrame f = new JFrame("Only input number characters" ); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); Container c = f.getContentPane(); test test1; c.add( test1 = new test(),BorderLayout.NORTH ); c.add( new test( 10 ),BorderLayout.CENTER ); test1.setMaxLen( 20 ); f.pack(); f.show(); }}class numDoc extends PlainDocument{ int maxLength=16; public numDoc(int maxLen) { maxLength=maxLen; } public numDoc(){} public void setMaxLen( int maxLength ) { this.maxLength = maxLength; } public int getMaxLen() { return maxLength; } public void insertString(int offset,String s,AttributeSet a)throws BadLocationException { int len=getLength(); String str=getText(0,len); if ( (str+s).length()>maxLength ) { Toolkit.getDefaultToolkit().beep(); return; } try { java.util.StringTokenizer tokens = new java.util.StringTokenizer( str+s,"\\" ); while( tokens.hasMoreElements() ) { Integer.parseInt( (String)tokens.nextElement() ); } } catch ( Exception e ) { Toolkit.getDefaultToolkit().beep(); return; } super.insertString(offset,s,a); }} 谢谢 Danger2000(飞鱼) 和 tomcatjava(小鱼儿) 2位哥哥昨晚我写了一个类,实现了上面的功能,不过这边分数很少,在这里分数都给 Danger2000(飞鱼)了http://expert.csdn.net/Expert/topic/3000/3000655.xml?temp=.2860224 中还有40分,请tomcatjava(小鱼儿) 过去一下,分数都给你此外http://expert.csdn.net/Expert/topic/3005/3005761.xml?temp=.5663874 中还有50,希望Danger2000(飞鱼) 和 tomcatjava(小鱼儿)帮忙解决全屏显示问题。 命令提示符提示程序包不存在 判断时间 请教一个正则替换的问题 一个日期转换问题 屏幕上输入的int型怎样转成日期型? 重大发现,怎么老是空指针呢?呵呵 系统变量到底存在什么地方? 读j2sdk源码,请各位给点建议! 哪位大哥帮帮忙,看看怎样修改数据库中的数据 Image简单碰撞检测的一个问题 能告诉我这怎样实现吗?? 如何使上传图像等比例缩小?UP有分
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;public class TestInputNumber {
public static void main(String[] args) {
FormatTestFrame frame = new FormatTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class FormatTestFrame extends JFrame {
public FormatTestFrame() {
setTitle("FormatTest");
setSize(300, 100); Container contentPane = getContentPane();
JLabel lblIntField = new JLabel("NumberFilter:");
JFormattedTextField intField
= new JFormattedTextField(new
InternationalFormatter(
NumberFormat.getIntegerInstance()) {
protected DocumentFilter getDocumentFilter() {
return filter;
}
private DocumentFilter filter = new IntFilter();
});
intField.setValue(new Integer(100));
intField.setColumns(10); JPanel mainPanel = new JPanel();
mainPanel.add(lblIntField);
mainPanel.add(intField);
contentPane.add(mainPanel, BorderLayout.CENTER);
}
class IntFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offset,
String string, AttributeSet attr)
throws BadLocationException {
StringBuffer buffer = new StringBuffer(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER)
buffer.deleteCharAt(i);
}
super.insertString(fb, offset, buffer.toString(), attr);
} public void replace(FilterBypass fb, int offset,
int length, String string, AttributeSet attr)
throws BadLocationException {
if (string != null) {
StringBuffer buffer = new StringBuffer(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch) && ch != '\\' && ch!=KeyEvent.VK_BACK_SPACE && ch!=KeyEvent.VK_ENTER)
buffer.deleteCharAt(i);
}
string = buffer.toString();
}
super.replace(fb, offset, length, string, attr);
}
}
}
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;public class test extends JTextField
{
private numDoc doc = null; public test() {
super(12);
setDocument( doc = new numDoc());
} public test(int maxLen)
{
super(12);
setDocument( doc = new numDoc(maxLen));
}
public void setMaxLen( int maxLen ) {
doc.setMaxLen( maxLen );
} public int getMaxLen() {
return doc.getMaxLen();
} public static void main( String args[] ) {
JFrame f = new JFrame("Only input number characters" );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();
test test1;
c.add( test1 = new test(),BorderLayout.NORTH );
c.add( new test( 10 ),BorderLayout.CENTER );
test1.setMaxLen( 20 );
f.pack();
f.show();
}
}class numDoc extends PlainDocument
{
int maxLength=16; public numDoc(int maxLen)
{
maxLength=maxLen;
}
public numDoc(){} public void setMaxLen( int maxLength ) {
this.maxLength = maxLength;
} public int getMaxLen() {
return maxLength;
} public void insertString(int offset,String s,AttributeSet a)throws BadLocationException
{
int len=getLength();
String str=getText(0,len);
if ( (str+s).length()>maxLength )
{
Toolkit.getDefaultToolkit().beep();
return;
}
try
{
java.util.StringTokenizer tokens = new java.util.StringTokenizer( str+s,"\\" );
while( tokens.hasMoreElements() ) {
Integer.parseInt( (String)tokens.nextElement() );
}
}
catch ( Exception e )
{
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,a);
}
}
昨晚我写了一个类,实现了上面的功能,不过这边分数很少,在这里分数都给 Danger2000(飞鱼)
了
http://expert.csdn.net/Expert/topic/3000/3000655.xml?temp=.2860224 中还有40分,请tomcatjava(小鱼儿) 过去一下,分数都给你此外http://expert.csdn.net/Expert/topic/3005/3005761.xml?temp=.5663874 中还有50,希望Danger2000(飞鱼) 和 tomcatjava(小鱼儿)帮忙解决全屏显示问题。