Basically I'm creating this simple login page that consist of couple of labels, two text boxes for inputs and two buttons but a couple of questions puzzle me. By using the if/else statement, it only limits the validation of 2 specific users for the program, thus I would like to create a scanner class to read the inputs from the database (supposingly a notepad) and compare it with the input from the two text boxes. However to no avail, I could neither find the way to implement the scanner in the existing codes, or save the input results (from the notepad) of the scanner to compare with the input from the text boxes. Included below are the original codes, and I'm hoping that you guys out there can come out with some suggestions or even better some improvement to the codes. Thanks in advance!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener
{
JButton login, exit;
JPanel panel1, panel2, panel3;
JLabel label1, label2, enterlabel;
final JTextField text1, text2;
Login() // constructor
{
enterlabel = new JLabel("Please key in usernmae and password.");
label1 = new JLabel();
label1.setText(" Username :");
text1 = new JTextField(10);
label2 = new JLabel();
label2.setText(" Password :");
text2 = new JPasswordField(10);
login = new JButton("Login");
exit = new JButton("Exit");
panel1 = new JPanel();
panel2 = new JPanel(new GridLayout(3,2));
panel3 = new JPanel();
panel1.add(enterlabel);
panel2.add(label1);
panel2.add(text1);
panel2.add(label2);
panel2.add(text2);
panel3.add(login);
panel3.add(exit);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
login.addActionListener(this);
ExitButtonHandler ehandler = new ExitButtonHandler(); // instantiate a handler
exit.addActionListener(ehandler); // register the handler
setTitle("Welcome To CIMB - Login Page");
}
public void actionPerformed(ActionEvent o)
{
String value1 = text1.getText();
String value2 = text2.getText();
if(value1.equals("chee yin") && value2.equals("123")||value1.equals("luen hong") && value2.equals("hong987"))
{
NextPage page = new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome :"+ value1);
page.getContentPane().add(label);
}
else {
JOptionPane.showMessageDialog(this, "Incorrect username or password. Please re-enter username and password", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
// inner classes for the button event handlers
class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame = new Login();
frame.setSize(350, 150);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
// end of inner class