简体   繁体   中英

Java Password field

Is it possible to create a passwordfield where you can get the text in java? Right now I tried something like

    Password = new JPasswordField();
    Password.setBounds(231, 134, 131, 23);
    frame.getContentPane().add(Password);    
    Password.getText();

but It won't work because the password is "private".

This is my code so far

    package Bank;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    public class BankLogin {
String usernameuser;
String passworduser;
ArrayList<String> Usernames = new ArrayList<String>();
ArrayList<String> Passwords = new ArrayList<String>();
private JFrame frame;
private JTextField Username;
private JPasswordField passwordField;
private JPasswordField Password;

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

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

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 508, 381);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);


    JPasswordField Password = new JPasswordField();
    Password.setBounds(231, 134, 131, 23);
    frame.getContentPane().add(Password);

    Username = new JTextField();
    Username.setBounds(231, 73, 131, 23);
    frame.getContentPane().add(Username);
    Username.setColumns(10);

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setBounds(109, 66, 112, 36);
    lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
    frame.getContentPane().add(lblUsername);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setBounds(109, 125, 112, 36);
    lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
    frame.getContentPane().add(lblPassword);


    JButton btnLogin = new JButton("Login");
    btnLogin.setBounds(109, 240, 89, 23);
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });
    frame.getContentPane().add(btnLogin);

    JButton btnRegister = new JButton("Register");
    btnRegister.setBounds(273, 240, 89, 23);
    btnRegister.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if((Username.getText()).length() != 0 && (Password.getPassword()).length !=0){
                JOptionPane.showMessageDialog(frame, "A basic JOptionPane messagessdfasdf dialog");
            }
            else{
                 Usernames.add(Username.getText());
                 Passwords.add(Password.getPassword());
                 JOptionPane.showMessageDialog(frame, "A basic JOptionPane message dialog");
            }
        }
    });
    frame.getContentPane().add(btnRegister);


}

} It's basically a login system. Right now I'm working on adding the password the user inputs into the array of passwords.

Gotta give it a object name and use get Password

JPasswordField pass = new JPasswordField();
pass.setBounds(231, 134, 131, 23);
frame.getContentPane().add(pass);    
pass.getPassword();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM