当前位置: 技术问答>java相关
用Java Applet 进行Web编程时,如何限制输入域中可输入字符的长度!解决后马上给分!!!
来源: 互联网 发布时间:2015-11-01
本文导语: 用Java Applet 进行Web编程时,如何限制输入域中可输入字符的长度!解决后马上给分!!! | create a new class FixLengthDocument extends PlainDocument, override public void insertString(int offs, String str, ...
用Java Applet 进行Web编程时,如何限制输入域中可输入字符的长度!解决后马上给分!!!
|
create a new class FixLengthDocument extends PlainDocument, override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
to check string length, for full example, see the following code snippet.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.text.*;
import java.awt.event.*;
public class TimeControl extends JPanel
{
private FixLenthTextField inYear;
private FixLenthTextField inMonth;
private FixLenthTextField inDay;
private FixLenthTextField inHour;
private FixLenthTextField inMinute;
private FixLenthTextField inSecond;
private JLabel sDeli;
public TimeControl()
{
createControl();
}
public void setColor(Color bkclr,Color foreclr)
{
super.setBorder(BorderFactory.createEmptyBorder());
super.setBackground(bkclr);
Container c=this;
Component[] components=c.getComponents();
if(components==null)
return;
int num=components.length;
for(int i=0;i0)
{
if(len>0)
{
len--;
scontent=scontent.substring(0,len);
if(isValidValue(scontent)>=0)
{
break;
}
}
else
break;
}
super.remove(0,super.getLength());
if(scontent!=null)
super.insertString(0,scontent,a);
}
protected void doBeep()
{
Toolkit.getDefaultToolkit().beep();
}
}
}
}
to check string length, for full example, see the following code snippet.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.text.*;
import java.awt.event.*;
public class TimeControl extends JPanel
{
private FixLenthTextField inYear;
private FixLenthTextField inMonth;
private FixLenthTextField inDay;
private FixLenthTextField inHour;
private FixLenthTextField inMinute;
private FixLenthTextField inSecond;
private JLabel sDeli;
public TimeControl()
{
createControl();
}
public void setColor(Color bkclr,Color foreclr)
{
super.setBorder(BorderFactory.createEmptyBorder());
super.setBackground(bkclr);
Container c=this;
Component[] components=c.getComponents();
if(components==null)
return;
int num=components.length;
for(int i=0;i0)
{
if(len>0)
{
len--;
scontent=scontent.substring(0,len);
if(isValidValue(scontent)>=0)
{
break;
}
}
else
break;
}
super.remove(0,super.getLength());
if(scontent!=null)
super.insertString(0,scontent,a);
}
protected void doBeep()
{
Toolkit.getDefaultToolkit().beep();
}
}
}
}