Java基础(Swing组件之密码框)
程序员文章站
2022-06-11 21:06:33
...
例如:
```handlebars
package Swing组件;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo10 extends JFrame {
public Demo10(){
setBounds(100,100,400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout( new FlowLayout());
JPasswordField jp = new JPasswordField();
jp.setColumns(20); //设置密码框长度,20个字符
jp.setFont(new Font("Arial",Font.BOLD,18)); //设置字体
jp.setEchoChar('#'); //设置回旋字符
c.add(jp);
jp.addActionListener(new ActionListener() { //添加动作监听,回车
@Override
public void actionPerformed(ActionEvent e) {
char ch[] = jp.getPassword(); //获取密码的字符数组
// String str = new String(ch);
System.out.println(ch);
jp.setText(""); //输入后情况
jp.requestFocus(); //获取光标
}
});
c.add(jp);
setVisible(true);
}
public static void main(String[] args) {
new Demo10();
}
}
运行结果:
上一篇: 广度优先搜索(BFS)求解最短路径问题
下一篇: Linux7如何设置静态IP的实例