gpt4 book ai didi

JAVA图形用户界面 : Can someone help me check why my button doesn't respond to it being clicked?

转载 作者:行者123 更新时间:2023-12-02 09:01:30 25 4
gpt4 key购买 nike

我是 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图形用户界面 : Can someone help me check why my button doesn't respond to it being clicked?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60128644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com