Swing JPasswordField类

JPasswordField类是一个专门处理密码功能并允许编辑单行文本的组件。

类声明

以下是javax.swing.JPasswordField类的声明 -

public class JPasswordField
   extends JTextField

类构造函数

编号 构造函数 描述
1 JPasswordField() 构造一个新的JPasswordField,它包含一个默认文档,起始文本字符串为null0列宽。
2 JPasswordField(Document doc, String txt, int columns) 构造一个新的JPasswordField,它使用给定的文本存储模型和给定的列数。
3 JPasswordField(int columns) 构造具有指定列数的新空JPasswordField
4 JPasswordField(String text) 构造一个使用指定文本初始化的新JPasswordField
5 JPasswordField(String text, int columns) 构造一个使用指定文本和列初始化的新JPasswordField

类方法

编号 方法 描述
1 void copy() 在当前外观上调用provideErrorFeedback,它通常会发出错误。
2 void cut() 在当前外观上调用provideErrorFeedback,它通常会发出错误。
3 boolean echoCharIsSet() 如果此JPasswordField具有用于回显的字符集,则返回true
4 AccessibleContext getAccessibleContext() 返回与此JPasswordField关联的AccessibleContext
5 char getEchoChar() 返回用于回显的字符。
6 char[] getPassword() 返回此TextComponent中包含的文本。
7 String getText() 已过时。从Java 2平台v1.2开始,由getPassword()取代。
8 String getUIClassID() 返回呈现此组件的L&F类的名称。
9 protected String paramString() 返回此JPasswordField的字符串表示形式。
10 void setEchoChar(char c) 设置此JPasswordField的回显字符。
11 void updateUI() 重新加载可插入的UI。

方法继承

该类继承以下类中的方法 -

  • javax.swing.JTextField
  • javax.swing.text.JTextComponent
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JPasswordField示例

使用编辑器创建以下Java程序 -


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JPasswordFieldExample {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public JPasswordFieldExample(){
      prepareGUI();
   }
   public static void main(String[] args){
       JPasswordFieldExample  swingControlDemo = new JPasswordFieldExample();      
      swingControlDemo.showPasswordFieldDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing JPasswordField示例");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showPasswordFieldDemo(){
      headerLabel.setText("Control in action: JPasswordField"); 

      JLabel namelabel= new JLabel("用户名: ", JLabel.RIGHT);
      JLabel passwordLabel = new JLabel("密码", JLabel.CENTER);
      final JTextField userText = new JTextField(6);
      final JPasswordField passwordText = new JPasswordField(6);      
      passwordText.setEchoChar('~');

      JButton loginButton = new JButton("登录");
      loginButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            String data = "用户名:" + userText.getText();
            data += ", 密码: " + new String(passwordText.getPassword()); 
            statusLabel.setText(data);        
         }
      }); 
      controlPanel.add(namelabel);
      controlPanel.add(userText);
      controlPanel.add(passwordLabel);       
      controlPanel.add(passwordText);
      controlPanel.add(loginButton);
      mainFrame.setVisible(true);  
   }
}

执行上面示例代码,得到以下结果:

JPasswordField


上一篇: Swing容器类 下一篇:无