I have a quick question. So below is a java program for an applet that provides
ID: 3567188 • Letter: I
Question
I have a quick question. So below is a java program for an applet that provides a login screen. I need the applet to say access granted once I type in the correct password (which is Seminole, UCF, jason, FSU, dorothy or USF) and then hit enter. And I need the applet to say access denied if I enter the wrong password and hit enter. The problem is the applet sais access denied as soon as I start typing and before i have hit enter. Would anyone know how to fix this? Below is my java applet application
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
/*class name Password for Applet*/
public class Password extends Applet implements KeyListener
{
JLabel label1,label2;
JTextField textfield;
JButton button;
/*ARRAY of valid passwords*/
String acceptablePasswords[]={"Seminole","UCF","jason","FSU","dorothy","USF"};
public void init()
{
/*setting the layout to FlowLayout*/
setLayout(new FlowLayout());
label1=new JLabel("Enter Password (and Click OK)");
add(label1);
textfield=new JTextField(15);
add(textfield);
button=new JButton("Ok");
add(button);
label2=new JLabel("");
add(label2);
/*adding listener to button*/
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String data=textfield.getText();
for(int i=0;i<acceptablePasswords.length;i++)
{
if(data.equals(acceptablePasswords[i]))
{
label2.setText("Access Granted");
return;
}
}
label2.setText("Access Denied");
}
});
/*Adding key listener to textfield on click of enter*/
textfield.addKeyListener(this);
textfield.setFocusable(true);
setSize(300,100);
setVisible(true);
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
String data=textfield.getText();
if(!data.equals(""))
{
for(int i=0;i<acceptablePasswords.length;i++)
{
if(data.equals(acceptablePasswords[i]))
{
label2.setText("Access Granted !!!!");
return;
}
}
label2.setText("Access Denied !!!!");
}
}
}
/* APPLET TAG
<APPLET CODE="Password.class" WIDTH="300" HEIGHT="100">
</APPLET>
*/
Explanation / Answer
//I have modified actionlistener method and keylistner method
//If u click ok, it will check whether its correct or not
//if u dont want the message to go on clicking a key jus remove the keylistener added to the text field
//comment if u hav any doubts
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Password extends Applet {
JLabel label1, label2;
JTextField textfield;
JButton button;
boolean match=false;
String acceptablePasswords[] = { "Seminole", "UCF", "jason", "FSU",
"dorothy", "USF" };
public void init() {
setLayout(new FlowLayout());
label1 = new JLabel("Enter Password (and Click OK)");
add(label1);
textfield = new JTextField(15);
add(textfield);
button = new JButton("Ok");
add(button);
label2 = new JLabel("");
add(label2);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String data = textfield.getText();
for (int i = 0; i < acceptablePasswords.length; i++) {
if (data.equals(acceptablePasswords[i])) {
match=true;
break;
}
}
if(match){label2.setText("Access Granted");match=false;}
else label2.setText("Access Denied");
}
});
textfield.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent arg0)
{label2.setText("");}
public void keyReleased(KeyEvent arg0) {}
public void keyPressed(KeyEvent arg0) {}
});
textfield.setFocusable(true);
setSize(300, 100);
setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.