JAVA GUI:有人可以帮我检查一下为什么我的按钮没有响应吗?
JAVA GUI: Can someone help me check why my button doesn't respond to it being clicked?
我是 JAVA GUI 的新手,我刚刚开始学习,我开始编写这个简单的身份验证框,但我不确定为什么我的 "fogotpassword" 按钮没有响应"else if (event.getSource()==forgotpassword)" 在方法 thehandler 中。单击它时没有任何反应。它下面的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);
}
}
}
}
}
代码似乎没有按预期工作,因为 actionPerformed
方法第二行中的 if
不包含检查忘记密码按钮是否为事件源的条件。您必须将此检查添加到外部 if 或将 else if (event.getSource()==forgotpassword)
移到该外部 if.
之外
我是 JAVA GUI 的新手,我刚刚开始学习,我开始编写这个简单的身份验证框,但我不确定为什么我的 "fogotpassword" 按钮没有响应"else if (event.getSource()==forgotpassword)" 在方法 thehandler 中。单击它时没有任何反应。它下面的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);
}
}
}
}
}
代码似乎没有按预期工作,因为 actionPerformed
方法第二行中的 if
不包含检查忘记密码按钮是否为事件源的条件。您必须将此检查添加到外部 if 或将 else if (event.getSource()==forgotpassword)
移到该外部 if.