I've been doing Java for a little while, but all of my small programs so far have had just one class. I'm currently working on a project, and I would like to use one class called GUI for the interface, and others for various functions, in this case the login code.
Right now, the login code is in the GUI class, my main class.
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnLogin)
{
String user = txtUser.getText();
char[] pass = txtPass.getPassword();
if (user.equals("John") && isPasswordCorrect(pass))
{
JOptionPane.showMessageDialog(null, "Access granted", "Login", JOptionPane.INFORMATION_MESSAGE);
Tabs.setEnabledAt(0,false);
Tabs.setEnabledAt(1,true);
Tabs.setSelectedIndex(1);
}
else
{
txtUser.setText("");
txtPass.setText("");
txtUser.requestFocusInWindow();
JOptionPane.showMessageDialog(null, "Login failed. Please check your username and password before trying again.", "Login Failed", JOptionPane.ERROR_MESSAGE);
}
//Zero out the possible password, for security.
Arrays.fill(pass, '0');
}
}
private static boolean isPasswordCorrect(char[] pass)
{
boolean isCorrect = true;
char[] correctPassword = { 'h', 'o', 'n', 'e', 'y', 'c', 'o', 'm', 'b' };
if (pass.length != correctPassword.length)
{
isCorrect = false;
}
else
{
isCorrect = Arrays.equals (pass, correctPassword);
}
//Zero out the password.
Arrays.fill(correctPassword,'0');
return isCorrect;
}
However, I would like this to be in the Login class to keep the GUI class strictly for the code of the interface itself. How do I take this action listener from GUI.class to Login.class so that is still functions when btnLogin is pressed? I'm not very well versed on using multiple classes.
Many thanks, any guidance would be appreciated.

New Topic/Question
Reply




MultiQuote








|