⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 passwordfield.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
package jp.co.ntl.swing;

import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

public class PasswordField extends JTextField {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public PasswordField(int figure) {
		super(figure);
		
		Document	doc = new PasswordDisplayDocument(figure);
		setDocument(doc);
		
		enableInputMethods(false);
	}
	
	public PasswordField() {
		super();
		
		Document	doc = new PasswordDisplayDocument(-1);
		setDocument(doc);
		
		enableInputMethods(false);
	}
	
	public String getText() {
		try {
			PasswordDisplayDocument	doc = (PasswordDisplayDocument)getDocument();
			return doc.getText(super.getText().length());
		} catch (BadLocationException e) {
			return "";
		}
	}
	
	private class PasswordDisplayDocument extends PlainDocument {
	    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private int				figure;
		private PasswordDocument	doc;
		
		public PasswordDisplayDocument(int figure) {
			this.figure = figure;
			this.doc = new PasswordDocument(figure);
		}
		
		public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
			String	aster;

    		if (str == null) {
    			return;
    		}
    		
    		if (figure >= 0) {
    			// 嵟戝寘悢偑擖椡偝傟偰偄傞偲偒
    			if (figure < getLength()) {
    				return;
    			}
    		
    			if (figure < getLength() + str.length()) {
    				return;
    			}
    		}
    		
    		for (int i = 0; i < str.length(); i++) {
    			char	c = str.charAt(i);
    			if (c < ' ' || c > 0x7f) {
    				return;
    			}
    		}
    		
    		doc.insertString(offs, str, a);
    		
    		aster = "";
    		for (int i = 0; i < str.length(); i++) {
    			aster += "*";
    		}
    		
    		super.insertString(offs, aster, a);
		}
		
		public String getText(int len) throws BadLocationException {
			return doc.getText(len);
		}
	}
	
	private class PasswordDocument extends PlainDocument {
	    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		public PasswordDocument(int figure) {
		}
		
		public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
    		super.insertString(offs, str, a);
		}
		
		public String getText(int len) throws BadLocationException {
			return getText(0, len);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -