hai...
i cant really think right now..
i need ur help..
i want to create a login page that user must enter correct user id and password.
and i have to set my own user id and password.
after ok is clicked, the program will check whether the user id and password is correct or not.
Then i have to prompt a suitable message to tell user whether he/she
has entered valid or invalid user id and password.
so far, i've made this :
CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class PasswordDemo extends JPanel implements ActionListener {
private static String OK = "ok";
private static String CANCEL = "cancel";
private JFrame controllingFrame; //needed for dialogs
private JPasswordField passwordField;
private JTextField useridField;
public PasswordDemo(JFrame f) {
//Use the default FlowLayout.
controllingFrame = f;
useridField = new JTextField(10);
passwordField = new JPasswordField(10);
useridField.setActionCommand(OK);
passwordField.setActionCommand(OK);
passwordField.addActionListener(this);
useridField.addActionListener(this);
JLabel label2 = new JLabel("User id: ");
label2.setLabelFor(useridField);
JLabel label = new JLabel("Password: ");
label.setLabelFor(passwordField);
JComponent buttonPane = createButtonPanel();
//Lay out everything.
JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
textPane.add(label);
textPane.add(label2);
textPane.add(useridField);
textPane.add(passwordField);
add(textPane);
add(buttonPane);
}
protected JComponent createButtonPanel() {
JPanel p = new JPanel(new GridLayout(0,1));
JButton okButton = new JButton("OK");
JButton cancelButton = new JButton("CANCEL");
okButton.setActionCommand(OK);
okButton.addActionListener(this);
cancelButton.setActionCommand(CANCEL);
cancelButton.addActionListener(this);
p.add(okButton);
p.add(cancelButton);
return p;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (OK.equals(cmd)) { //Process the password.
char[] input = useridField.[color=#FFFF66]getUserid[/color]() && passwordField.getPassword();
if (isUseridCorrect(input) && isPasswordCorrect(input)){
JOptionPane.showMessageDialog(controllingFrame,
"Success! You've typed the right user id.Please proceed to enter your password");
} else {
JOptionPane.showMessageDialog(controllingFrame,
"Invalid user id. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
Arrays.fill(input, '0');
useridField.selectAll();
passwordField.selectAll();
resetFocus();
} else {
JOptionPane.showMessageDialog(controllingFrame,
"Thank you");
}
}
private static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'a', 'm', 'y', '2', '0', '1', '2' };
if (input.length != correctPassword.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword);
}
//Zero out the password.
Arrays.fill(correctPassword,'0');
return isCorrect;
}
private static boolean isUseridCorrect(char[] input) {
boolean isCorrect = true;
char[] correctUserid = { 'm', 'i', 'n', 't'};
if (input.length != correctUserid.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctUserid);
}
//Zero out the user id
Arrays.fill(correctUserid,'0');
return isCorrect;
}
//Must be called from the event dispatch thread.
protected void resetFocus() {
useridField.requestFocusInWindow();
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final PasswordDemo newContentPane = new PasswordDemo(frame);
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
but then,
it says cannot find symbol method getUserid()
hmph...
i dont really know if im doing the right thing..
so..
pleaassseee guide me!!!