06 May 2016

Hide and show password login in java swing Eclipse




package pass;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;

public class Pass {

 private JFrame frame;
 private JPasswordField txtPass;
 private JLabel txtCheck;
 private JLabel lblPassword;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     Pass window = new Pass();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public Pass() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 302, 176);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);
  
  txtPass = new JPasswordField();
  txtPass.setEchoChar('*');
  txtPass.setBounds(112, 22, 126, 24);
  frame.getContentPane().add(txtPass);
  
  txtCheck = new JLabel("See password");
  txtCheck.setBounds(122, 63, 116, 23);
  frame.getContentPane().add(txtCheck);
  
  lblPassword = new JLabel("Password:");
  lblPassword.setBounds(26, 27, 76, 14);
  frame.getContentPane().add(lblPassword);
  
  JCheckBox checkBox = new JCheckBox();
  checkBox.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    if(checkBox.isSelected()){
     txtCheck.setText("Hide password");
     txtPass.setEchoChar((char)0);
    }else{
     txtCheck.setText("See password");
     txtPass.setEchoChar('*');
    }
   }
  });
  checkBox.setBounds(81, 63, 31, 23);
  frame.getContentPane().add(checkBox);

 }

}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang