构造一般的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);
			}
		});
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值