stdebernardi
asked on
Swing applet with a JTextField asking user for a password
Hi,
I have to Create a Swing applet that asks a user to enter a password into a JTextField and to then press [Enter].
Compare the password to "Rosebud"; if it marches, display "Access Granted", if not display "Access Denied.
I tried to write a program but everytime I run it, whenever I enter the correct password or the wrong one it comes out as "access denied".
Here is my code: (Hope you can help)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPasswordA extends JApplet implements ActionListener
{
JLabel greeting = new JLabel("Please, enter password");
Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
JButton pressMe = new JButton("Press ME");
JTextField answer = new JTextField("",10);
FlowLayout flow = new FlowLayout();
public void init()
{
greeting.setFont(bigFont);
Container con = getContentPane();
con.add(greeting);
con.setLayout(flow);
con.add(answer);
con.add(pressMe);
pressMe.addActionListener( this);
answer.addActionListener(t his);
answer.requestFocus();
}
public void actionPerformed(ActionEven t thisEvent)
{
Object source = thisEvent.getSource();
if(source == ("Rosebud"))
{
String name = answer.getText();
System.out.println("Access granted");
}
else if(source instanceof JTextField);
{
String name = answer.getText();
System.out.println("Access denied");
}
}
}
I have to Create a Swing applet that asks a user to enter a password into a JTextField and to then press [Enter].
Compare the password to "Rosebud"; if it marches, display "Access Granted", if not display "Access Denied.
I tried to write a program but everytime I run it, whenever I enter the correct password or the wrong one it comes out as "access denied".
Here is my code: (Hope you can help)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPasswordA extends JApplet implements ActionListener
{
JLabel greeting = new JLabel("Please, enter password");
Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
JButton pressMe = new JButton("Press ME");
JTextField answer = new JTextField("",10);
FlowLayout flow = new FlowLayout();
public void init()
{
greeting.setFont(bigFont);
Container con = getContentPane();
con.add(greeting);
con.setLayout(flow);
con.add(answer);
con.add(pressMe);
pressMe.addActionListener(
answer.addActionListener(t
answer.requestFocus();
}
public void actionPerformed(ActionEven
{
Object source = thisEvent.getSource();
if(source == ("Rosebud"))
{
String name = answer.getText();
System.out.println("Access
}
else if(source instanceof JTextField);
{
String name = answer.getText();
System.out.println("Access
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's great.
Thank you so much.
I thought about using a JPasswordField but the requirement was a JTextField, I don't know why, a JPasswordField would have been more realistic. Oh well.
Thanks again.
Thank you so much.
I thought about using a JPasswordField but the requirement was a JTextField, I don't know why, a JPasswordField would have been more realistic. Oh well.
Thanks again.
public void actionPerformed(ActionEven
{
Object source = thisEvent.getSource();
if (source instanceof JTextField)
{
String name = answer.getText();
if (name.equals("Rosebud")) {
System.out.println("Access
}
else
{
System.out.println("Access
}
}
}