Click to See Complete Forum and Search --> : First applet


barder
February 29th, 2000, 07:46 PM
I have to create an applet that asks the user for a passwwd that needs to match "steak". When the user enters the passwd it returns the message access is denied no matter what password is entered. Can some tell me what is wrong?


This is my code:


import java.applet.*;

import java.awt.*;

import java.awt.event.*;


public class Password2a extends Applet implements ActionListener

{

static final public String PASSWORD = "steak";

Label question = new Label("Please enter the password.");

Font bigFont = new Font("Courier", Font.BOLD,20);

Button Enter = new Button("Enter");

TextField password = new TextField(" ",10);

public void init()

{

question.setFont(bigFont);

add(question);

add(password);

add(Enter);

Enter.addActionListener(this);

password.addActionListener(this);

password.requestFocus();

}

public void actionPerformed(ActionEvent Enter)

{

String name = password.getText();

Label newPassword = new Label (" ");

if (name == PASSWORD)

newPassword.setText("Access Granted");

else

newPassword.setText("Access Denied");

add(newPassword);

invalidate();

validate();
}

}

kib63613
March 1st, 2000, 03:34 AM
you may try this way :
if(name.equals(PASSWORD)) {
}
else {
}
instead the instruction , if(name==PASSWORD)
good luck
Alfred Wu