构造一般的JPasswordField组件

构造一般的JPasswordField组件:

JPasswordField的构造函数和JTextField的构造函数几乎一模本样,唯一不同的是在JPasswordField输入时字符会以屏蔽字符的 类型表示。我们来看下面这个范例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JPasswordField1{

    public static void main(String args[])    {

    	JFrame f = new JFrame("JPasswordField1");
    	Container contentPane = f.getContentPane();
    	contentPane.setLayout(new BorderLayout());

    	JPanel p1 = new JPanel();        
    	//p1.setLayout(new GridLayout(4,2));
    	p1.setLayout(new GridBagLayout());
    	GridBagConstraints gbc = new GridBagConstraints();
    	gbc.anchor = GridBagConstraints.WEST; //设定Layout的位置
    	gbc.insets = new Insets(2,2,2,2); //设定与边界的距离(上,左,下,右)
    	
    	p1.setBorder(BorderFactory.createTitledBorder("您的基本数据"));         
    	JLabel l1 = new JLabel("姓名:");        
    	JLabel l2 = new JLabel("性别:");        
    	JLabel l3 = new JLabel("身高:");        
    	JLabel l4 = new JLabel("体重:");        
    	JPasswordField t1 = new JPasswordField();
    	JPasswordField t2 = new JPasswordField(2);
    	JPasswordField t3 = new JPasswordField(" 175cm");
    	JPasswordField t4 = new JPasswordField(" 50kg太瘦了",10);
 
	t1.setPreferredSize(t1.getPreferredSize());    	
    	gbc.gridy=1;
    	gbc.gridx=0;
    	p1.add(l1,gbc);
    	gbc.gridx=1;
    	p1.add(t1,gbc);
    	gbc.gridy=2;
    	gbc.gridx=0;
    	p1.add(l2,gbc);
    	gbc.gridx=1;
    	p1.add(t2,gbc);
    	gbc.gridy=3;
    	gbc.gridx=0;        
   	p1.add(l3,gbc);
   	gbc.gridx=1;
    	p1.add(t3,gbc);
    	gbc.gridy=4;
    	gbc.gridx=0;        
    	p1.add(l4,gbc);
    	gbc.gridx=1;
    	p1.add(t4,gbc);        
    	
    	contentPane.add(p1);        
    	f.pack();        
    	f.show();        
    	f.addWindowListener(new WindowAdapter() {            
    		public void windowClosing(WindowEvent e) {                    
    			System.exit(0);            
    		}        
    	});    
    }
}

我们可以看到默认的字符串都以屏蔽字符"*"来表示,"*"字符是JPasswordField默认的屏蔽字符,我们可以利用JPasswordField 提供的setEchoChar()方法来改用其他字符来作为屏蔽字符。我们来看下面的范例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JPasswordField2 {

	public static void main(String args[]) {

		JFrame f = new JFrame("JPasswordField2");
		Container contentPane = f.getContentPane();
		contentPane.setLayout(new BorderLayout());

		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(4, 2));
		p1.setBorder(BorderFactory.createTitledBorder("您的基本数据"));
		JLabel l1 = new JLabel("姓名:");
		JLabel l2 = new JLabel("性别:");
		JLabel l3 = new JLabel("身高:");
		JLabel l4 = new JLabel("体重:");
		JPasswordField t1 = new JPasswordField(10);
		JPasswordField t2 = new JPasswordField(2);
		JPasswordField t3 = new JPasswordField(" 175cm");
		JPasswordField t4 = new JPasswordField(" 50kg太瘦了", 10);

		t1.setEchoChar('#');
		t2.setEchoChar('%');
		t3.setEchoChar('&');
		t4.setEchoChar('M');

		p1.add(l1);
		p1.add(t1);
		p1.add(l2);
		p1.add(t2);
		p1.add(l3);
		p1.add(t3);
		p1.add(l4);
		p1.add(t4);

		contentPane.add(p1);
		f.pack();
		f.show();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
}

你可能感兴趣的:(JPasswordField)