Я новичок в JAVA GUI, и я только начал учиться, я начал писать это простое окно аутентификации, и я не уверен, почему моя кнопка "fogotpassword" не отвечает в строке "иначе, если ( event.getSource () == Forgotpassword) "в обработчике метода. Ничего не происходит, когда я нажимаю на него. System.out.println ("s") под ним не печатается.
Помощь будет очень признательна :). Хорошего дня.
public class miniProject extends JFrame
{
private JTextField UsernameBox;
private JPasswordField passwordField;
private JButton forgotpassword;
private JButton buttonLogin;
public miniProject()
{
super("Login Page");
setLayout(new GridLayout(3, 2,5,10));
setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JLabel username = new JLabel(" Username:");
//username.setEditable(false);
add(username);
UsernameBox = new JTextField(10);
add(UsernameBox);
//item3.setEditable(false);
JLabel passowrd = new JLabel(" Password:");
add(passowrd);
passwordField = new JPasswordField(10);
add(passwordField);
buttonLogin = new JButton("Login");
add(buttonLogin);
forgotpassword = new JButton("Forgot My Password");
add(forgotpassword);
thehandler handler = new thehandler();
UsernameBox.addActionListener(handler);
passwordField.addActionListener(handler);
buttonLogin.addActionListener(handler);
forgotpassword.addActionListener(handler);
}
private class thehandler implements ActionListener
{
int i =5;
public void actionPerformed(ActionEvent event)
{
String string = "";
if (event.getSource()== passwordField || event.getSource()== UsernameBox ||event.getSource()==buttonLogin)
{
String password="";
if(passwordField.getPassword().length != 0 && UsernameBox.getText().length()!=0 )
{
for (char a :passwordField.getPassword())
{
password +=a;
}
password = password.trim();
if (password.equals("kyle") && UsernameBox.getText().equals("marcus"))
{
string = String.format("Welcome Back!", event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
else
{
string = String.format("Incorrect Password\n "+i+" more attempts remaining until your account is locked", event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
i--;
}
}
else if (event.getSource()==forgotpassword)
{
System.out.println("s");
String input = JOptionPane.showInputDialog("Enter your email");
if (input.length()!=0)
{
JOptionPane.showInternalMessageDialog(null, "Check your email to reset password!", "Password Reset",JOptionPane.PLAIN_MESSAGE);
}
}
else if (passwordField.getPassword().length == 0)
{
string = String.format("You didn't enter your password!",event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
else if (UsernameBox.getText().length()==0)
{
string = String.format("You didn't enter your username!",event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
}
}
}
}