怎么设置JTextField的maxLenght setFocus maxLengh自己写,setFocus直接调。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TO stonegump(龙飞虎) setFocus直接调,不知道怎么调用啊. package TextBox;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.lang.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.PlainDocument;import TextBox.*;public class TextBox extends JTextField{ private int iMaxLen; public TextBox() { super(); LoadEvent(); } public void setMaxLength(int iMaxLength) { iMaxLen = iMaxLength; } public void LoadEvent() { this.addFocusListener(new FocusListener(){ public void focusLost(FocusEvent e) { LostFocus(); } public void focusGained(FocusEvent e) { GetFocus(); } }); this.addKeyListener(new MaxLengthKeyListener()); } public void LostFocus() { if(!IsNumber(this.getText())) { this.requestFocus(); } } public void GetFocus() { this.selectAll(); } public static boolean IsNumber(String sTmp) { String sNumber[]={"0","1","2","3","4","5","6","7","8","9"}; String sComp=""; for(int j=0;j<sTmp.length();j++ ) { sComp=sTmp.substring(j,j+1); for(int i=0;i <10 ;i++ ) { if( sComp.equals(sNumber[i]) ) { break; } if ( i == 9) { return false; } } } return true; } protected Document createDefaultModel() { return new MaxLengthDocument(); } class MaxLengthDocument extends PlainDocument { private int added = 0; public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException { if (arg0 < iMaxLen) { super.insertString(arg0, arg1, arg2); } } } class MaxLengthKeyListener implements KeyListener { private String oldString; public void keyPressed(KeyEvent arg0) { checkMaxLength(); } public void keyReleased(KeyEvent arg0) { checkMaxLength(); } public void keyTyped(KeyEvent arg0) { checkMaxLength(); } private void checkMaxLength() { String checkString = getText(); byte[] byteLength = checkString.getBytes(); if (byteLength.length > iMaxLen) { setText(oldString); } else { oldString = checkString; } } }} java中的|符号 JavaSwing排序动画源代码分享 请问为什么这个程序没有报错并且也没有运行结果呢?谢谢! 唉,再次紧急麻烦各位兄弟,使用正则有没有办法处理这个问题 PrintScreen这个键去截图啊 没分,求解,烦人的各种文档。 有上过尚学堂的朋友吗? applet入门问题,帮忙看看! 请问在更新数据库操作,如何实现事务。 安装jre8以后出现一个大问题 高分求解:java.lang.IllegalStateException: getOutputStream() has already been called for this response 画面合并的问题
setFocus直接调,不知道怎么调用啊.
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import TextBox.*;public class TextBox extends JTextField
{
private int iMaxLen; public TextBox()
{
super();
LoadEvent();
} public void setMaxLength(int iMaxLength)
{
iMaxLen = iMaxLength;
}
public void LoadEvent()
{
this.addFocusListener(new FocusListener(){
public void focusLost(FocusEvent e)
{
LostFocus();
}
public void focusGained(FocusEvent e)
{
GetFocus();
}
}); this.addKeyListener(new MaxLengthKeyListener());
}
public void LostFocus()
{
if(!IsNumber(this.getText()))
{
this.requestFocus();
}
} public void GetFocus()
{
this.selectAll();
} public static boolean IsNumber(String sTmp)
{
String sNumber[]={"0","1","2","3","4","5","6","7","8","9"};
String sComp=""; for(int j=0;j<sTmp.length();j++ )
{
sComp=sTmp.substring(j,j+1);
for(int i=0;i <10 ;i++ )
{
if( sComp.equals(sNumber[i]) )
{
break;
}
if ( i == 9)
{
return false;
}
}
}
return true;
} protected Document createDefaultModel()
{
return new MaxLengthDocument();
} class MaxLengthDocument extends PlainDocument
{
private int added = 0; public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException
{
if (arg0 < iMaxLen)
{
super.insertString(arg0, arg1, arg2);
}
}
} class MaxLengthKeyListener implements KeyListener
{
private String oldString; public void keyPressed(KeyEvent arg0)
{
checkMaxLength();
} public void keyReleased(KeyEvent arg0)
{
checkMaxLength();
} public void keyTyped(KeyEvent arg0)
{
checkMaxLength();
} private void checkMaxLength()
{
String checkString = getText();
byte[] byteLength = checkString.getBytes();
if (byteLength.length > iMaxLen)
{
setText(oldString);
} else {
oldString = checkString;
}
}
}
}