?

import ?javax.swing.text.AttributeSet;
import ?javax.swing.text.BadLocationException;
import ?javax.swing.text.PlainDocument;

/**
*?<p>DBC?case?for?TextField</p>
*?
*?
@author ?wt
*?
@version ?0.01
*/

public ? class ?DBCDocument? extends ?PlainDocument {
????
private ? int ?maxchars_? = ? 32 ;
????
????
/**
?????*?
?????*?
@param ?maxchars?
?????
*/

????
public ? void ?setMaxChars( int ?maxchars)? {
????????maxchars_?
= ?maxchars;
????}

????
public ? void ?insertString( int ?offs,?String?str,?AttributeSet?a)? throws ?BadLocationException? {
????????
if ?(getLength()? + ?str.length()? > ?maxchars_)? {
????????????
return ;
????????}

????????String?newstr?
= ? "" ;
????????
for ?( int ?i? = ? 0 ;?i? < ?str.length();?i ++ )? {
????????????
char ?now? = ?str.charAt(i);
????????????
if ?(now? >= ? ' \u0020 ' ? && ?now? <= ? ' \u007e ' )? {
????????????????newstr?
+= ?now;
????????????}
? else ? if ?(now? == ? ' \u3000 ' )? {
????????????????newstr?
+= ? ' \u0020 ' ;
????????????}
? else ? if ?(now? >= ? ' \uff01 ' ? && ?now? <= ? ' \uff5e ' )? {
????????????????newstr?
+= ?( char )(now? - ? 0xfee0 );
????????????}
? else ? {
????????????????
return ;
????????????}

????????}

????????????
super .insertString(offs,?newstr,?a);
????}

// ????public?static?void?main(String[]?args)?{
// ????????JFrame?f?=?new?JFrame();
// ????????JTextField?txt?=?new?JTextField(50);
// ????????txt.setDocument(new?DBCDocument());
// ????????f.getContentPane().add(txt);
// ????????f.pack();
// ????????f.show();
// ????????f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// ????}
}