简体   繁体   中英

How do i make a JPassword Field launch an application

JPanel p = new JPanel();              

JTextField userName = new JTextField(10);

JPasswordField passWord = new JPasswordField(10);                 

p.add(new JLabel("Username:"));
p.add(userName);
p.add(new JLabel("Password: "));
p.add(passWord);

JOptionPane.showConfirmDialog(null, p, "Username and password: ", JOptionPane.OK_CANCEL_OPTION);

My question is how do i make it so The Username is username and the password is password and if you type in the correct username and password it launches the application if not it closes it?

In your gui,

int selectedOption = JOptionPane.showConfirmDialog(null, p, "Username and password: ", JOptionPane.YES_NO_OPTION); 
if (selectedOption == JOptionPane.YES_OPTION) {
if (userName.getText().equals("abc") && passWord.getText().equals("abc")) {
//do some action, login success
}
else
{
//do some action, login fail
}
}//end if
else
{
//do some action, means user cancel the login
}

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