首页 > 代码库 > Java Swing界面编程(19)---密码输入组件:JPasswordField

Java Swing界面编程(19)---密码输入组件:JPasswordField

JTextField是使用明文方式进行数据显示的,如果现在需要将回显的内容设置成其他字符,则可以使用JPasswordField类。

package com.beyole.util;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

public class test18 {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Crystal");// 实例化窗体对象
		JPasswordField jPasswordField1 = new JPasswordField();// 定义密文框
		JPasswordField jPasswordField2 = new JPasswordField();// 定义密文框
		jPasswordField2.setEchoChar('#');// 设置回显字符为#
		JLabel label1 = new JLabel("默认的回显:");
		JLabel label2 = new JLabel("设置的回显#:");
		label1.setBounds(10, 10, 100, 20);
		label2.setBounds(10, 40, 100, 20);
		jPasswordField1.setBounds(110, 10, 80, 20);
		jPasswordField2.setBounds(110, 40, 50, 20);
		frame.setLayout(null);
		frame.add(label2);
		frame.add(label1);
		frame.add(jPasswordField2);
		frame.add(jPasswordField1);
		frame.setSize(300, 100);
		frame.setLocation(300, 200);
		frame.setVisible(true);
	}
}

程序截图: