📜  Java摇摆 | JPasswordField

📅  最后修改于: 2022-05-13 01:55:39.058000             🧑  作者: Mango

Java摇摆 | JPasswordField

PasswordField 是 javax.swing 包的一部分。 JPasswordField 类是一个组件,它允许编辑单行文本,其中视图指示输入的内容不显示实际字符。 JPasswordField 继承了 javax.swing 包中的 JTextField 类。
该类的构造函数是:

  1. JPasswordField() :创建新 PasswordField 的构造函数
  2. JPasswordField(int columns) :创建具有指定列数的新的空 PasswordField 的构造函数。
  3. JPasswordField(String Password) :创建一个新的空密码字段的构造函数,用给定的字符串初始化。
  4. JPasswordField(String Password, int columns) :使用给定字符串和指定列数创建一个新的空 PasswordField 的构造函数。
  5. JPasswordField(Document doc, String Password, int columns) :创建使用给定文本存储模型和给定列数的 Passwordfield 的构造函数。

JPasswordField 的常用方法:

  1. char getEchoChar() :返回用于在 JPasswordField 中回显的字符。
  2. setEchoChar(char c) : 设置 JPasswordField 的回显字符。
  3. String getPassword() :返回 JPasswordField 中包含的文本。
  4. String getText() :返回 JPasswordField 中包含的文本。

1.程序使用JTextField和JPasswordField输入名称和密码

Java
// Java Program to enter name and password
// using JTextField and JPasswordField
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener {
    // JTextField
    static JTextField t;
 
    // JPasswordField
    static JPasswordField pass;
 
    // JFrame
    static JFrame f;
 
    // JButton
    static JButton b;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("textfield");
 
        // create a label to display text
        l = new JLabel("nothing entered");
 
        // create a new button
        b = new JButton("submit");
 
        // create a object of the text class
        text te = new text();
 
        // addActionListener to button
        b.addActionListener(te);
 
        // create a object of JTextField with 16 columns and initial text
        t = new JTextField("enter name", 16);
 
        // create a object of passwordField with 16 columns
        pass = new JPasswordField(16);
 
        // create an object of font type
        Font fo = new Font("Serif", Font.ITALIC, 20);
 
        // set the font of the textfield
        t.setFont(fo);
 
        // create a panel to add buttons and textfield
        JPanel p = new JPanel();
 
        // add buttons and textfield to panel
        p.add(t);
        p.add(pass);
        p.add(b);
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            l.setText("name = " + t.getText() + "\t Password = " + pass.getText());
 
            // set the text of field to blank
            t.setText("  ");
 
            // set the text of password field to blank
            pass.setText("");
        }
    }
}


Java
// Java Program to change the echo character of
// JPasswordField and set initial text for password field
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener, FocusListener {
    // JTextField
    static JTextField t;
 
    // JPasswordField
    static JPasswordField pass;
 
    // JFrame
    static JFrame f;
 
    // JButton
    static JButton b;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("textfield");
 
        // create a label to display text
        l = new JLabel("nothing entered");
 
        // create a new button
        b = new JButton("submit");
 
        // create a object of the text class
        text te = new text();
 
        // addActionListener to button
        b.addActionListener(te);
 
        // create a object of JTextField with 16 columns and initial text
        t = new JTextField("enter name", 16);
 
        // create a object of passwordField with 16 columns
        pass = new JPasswordField(16);
 
        // add FocusListener to passwordField
        pass.addFocusListener(te);
 
        // set the echo character of the password field
        pass.setEchoChar((char)0);
 
        // set initial text for password field
        pass.setText("enter password");
 
        // set the echo character of the password field
        // create an object of font type
        Font fo = new Font("Serif", Font.ITALIC, 20);
 
        // set the font of the textfield
        t.setFont(fo);
 
        // create a panel to add buttons and textfield
        JPanel p = new JPanel();
 
        // add buttons and textfield to panel
        p.add(t);
        p.add(pass);
        p.add(b);
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
    // flag to set the text to blank for the first time when the component gets focus
    boolean flag = true;
 
    // events of focus listener
    // when focus is gained
    public void focusGained(FocusEvent e)
    {
        if (flag) {
            // set a definite echo char
            pass.setEchoChar('*');
 
            // only set the text to blank for 1st time
            // set the text to blank
            pass.setText("");
            flag = false;
        }
    }
 
    // when the focus is lost
    public void focusLost(FocusEvent e)
    {
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            l.setText("name = " + t.getText() + "\t, Password = " + pass.getText());
 
            // set the text of field to blank
            t.setText("  ");
 
            // set the text of password field to blank
            pass.setText("");
        }
    }
}


输出 :

2.更改JPasswordField的回显字符并设置密码字段的初始文本的程序

Java

// Java Program to change the echo character of
// JPasswordField and set initial text for password field
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener, FocusListener {
    // JTextField
    static JTextField t;
 
    // JPasswordField
    static JPasswordField pass;
 
    // JFrame
    static JFrame f;
 
    // JButton
    static JButton b;
 
    // label to display text
    static JLabel l;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("textfield");
 
        // create a label to display text
        l = new JLabel("nothing entered");
 
        // create a new button
        b = new JButton("submit");
 
        // create a object of the text class
        text te = new text();
 
        // addActionListener to button
        b.addActionListener(te);
 
        // create a object of JTextField with 16 columns and initial text
        t = new JTextField("enter name", 16);
 
        // create a object of passwordField with 16 columns
        pass = new JPasswordField(16);
 
        // add FocusListener to passwordField
        pass.addFocusListener(te);
 
        // set the echo character of the password field
        pass.setEchoChar((char)0);
 
        // set initial text for password field
        pass.setText("enter password");
 
        // set the echo character of the password field
        // create an object of font type
        Font fo = new Font("Serif", Font.ITALIC, 20);
 
        // set the font of the textfield
        t.setFont(fo);
 
        // create a panel to add buttons and textfield
        JPanel p = new JPanel();
 
        // add buttons and textfield to panel
        p.add(t);
        p.add(pass);
        p.add(b);
        p.add(l);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
    // flag to set the text to blank for the first time when the component gets focus
    boolean flag = true;
 
    // events of focus listener
    // when focus is gained
    public void focusGained(FocusEvent e)
    {
        if (flag) {
            // set a definite echo char
            pass.setEchoChar('*');
 
            // only set the text to blank for 1st time
            // set the text to blank
            pass.setText("");
            flag = false;
        }
    }
 
    // when the focus is lost
    public void focusLost(FocusEvent e)
    {
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            l.setText("name = " + t.getText() + "\t, Password = " + pass.getText());
 
            // set the text of field to blank
            t.setText("  ");
 
            // set the text of password field to blank
            pass.setText("");
        }
    }
}


注意:上述程序可能无法在使用离线 IDE 的在线编译器中运行。
程序员可以根据需要更改密码的默认文本和列数。