This applet should test the success of user entry (id and password). if they are correct, change headerLabel to "Successful", if false it should display "Unsuccessfull" and then clear both text fields and reset the focus.
problem is even if you enter wrong id/pwd it displays Login successful. and it doesn't clear text fields. Please help!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class PasswordApplet extends Applet implements ActionListener
{
//Declaring variables
String id, password;
boolean success;
String idArray[] = {"id", "admin"};
String passwordArray[] = {"password", "login"};
Label nameLabel = new Label("Name - Date");
//Create components for applet
Label headerLabel = new Label("Please type your ID and Password");
Label idLabel = new Label("ID:");
TextField idField = new TextField(8);
Label passwordLabel = new Label("Password:");
TextField passwordField = new TextField(8);
Button loginButton = new Button("Login");
public void init()
{
//Set color, layout, and add components
setBackground(Color.orange);
setLayout(new FlowLayout(FlowLayout.LEFT,50,30));
add(nameLabel);
add(headerLabel);
add(idLabel);
add(idField);
idField.requestFocus();
add(passwordLabel);
add(passwordField);
passwordField.setEchoChar('*');
add(loginButton);
loginButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//Assigning data
id = idField.getText();
password = passwordField.getText();
success = false;
//Sequential search
for (int i=0; i<idArray.length; i++)
{
if ((idArray[i].compareTo(id)==0) && (passwordArray[i].compareTo(password)==0)) success=true;
}
if (success=true) headerLabel.setText("Login Successful");
else
{
headerLabel.setText("Unsuccessful, Try Again");
idField.setText("");
//passwordField.setText("");
//idField.requestFocus();
}
repaint();
}
}
Question
a1kashur
I'm having a little trouble with this java pgm.
This applet should test the success of user entry (id and password). if they are correct, change headerLabel to "Successful", if false it should display "Unsuccessfull" and then clear both text fields and reset the focus.
problem is even if you enter wrong id/pwd it displays Login successful. and it doesn't clear text fields. Please help!
import java.awt.*; import java.applet.*; import java.awt.event.*; public class PasswordApplet extends Applet implements ActionListener { //Declaring variables String id, password; boolean success; String idArray[] = {"id", "admin"}; String passwordArray[] = {"password", "login"}; Label nameLabel = new Label("Name - Date"); //Create components for applet Label headerLabel = new Label("Please type your ID and Password"); Label idLabel = new Label("ID:"); TextField idField = new TextField(8); Label passwordLabel = new Label("Password:"); TextField passwordField = new TextField(8); Button loginButton = new Button("Login"); public void init() { //Set color, layout, and add components setBackground(Color.orange); setLayout(new FlowLayout(FlowLayout.LEFT,50,30)); add(nameLabel); add(headerLabel); add(idLabel); add(idField); idField.requestFocus(); add(passwordLabel); add(passwordField); passwordField.setEchoChar('*'); add(loginButton); loginButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { //Assigning data id = idField.getText(); password = passwordField.getText(); success = false; //Sequential search for (int i=0; i<idArray.length; i++) { if ((idArray[i].compareTo(id)==0) && (passwordArray[i].compareTo(password)==0)) success=true; } if (success=true) headerLabel.setText("Login Successful"); else { headerLabel.setText("Unsuccessful, Try Again"); idField.setText(""); //passwordField.setText(""); //idField.requestFocus(); } repaint(); } }Link to comment
Share on other sites
2 answers to this question
Recommended Posts