Java 密码框 JPasswordField

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

class text2 extends JFrame{
	
	public text2() {
		// TODO Auto-generated constructor stub
		setBounds(400, 100, 400, 100);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		
		//设置密码框长度
//		JPasswordField jPasswordField = new JPasswordField(20);
		
		JPasswordField jPasswordField = new JPasswordField();
		jPasswordField.setColumns(20);
//		jPasswordField.setFont(new Font("Arial",Font.BOLD,20));
		jPasswordField.setEchoChar('\u235d');//设置回显字符或直接设置字符 单引号
		
		
		//添加动作监听 回车
		jPasswordField.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				char str[] = jPasswordField.getPassword();
				String string = new String(str);
				System.out.println(string);
			}
		});
		
		
		container.add(jPasswordField);
		
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new text2();
	}
}

 

你可能感兴趣的:(java)