The following applet produces a screen like the one below:
A JPasswordField works exactly like a JTextField.
Declare the JPasswordField. (Globally, because you need to get data from it) | JPasswordField p; |
Construct it. Make it "5" long. (In Init) | p = new JPasswordField (5); |
Add the JPasswordField to the applet. (In Init) | add (p); |
Get the text out of the JPasswordField. (In ActionPerformed) | p.getText (); |
Test if the text is correct (in this case, equals "computer") (In ActionPerformed) | if (p.getText ().equals ("computer")) |
Clear out the JPasswordField (In ActionPerformed) | p.setText (""); |
More information can be found at:
http://java.sun.com/docs/books/tutorial/uiswing/components/passwordfield.html
This is the complete code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PasswordField extends Applet implements ActionListener
{
JPasswordField p;
public void init ()
{ //orient, min, max
p = new JPasswordField (5);
add (p);
JButton done = new JButton ("Done");
done.setActionCommand ("Done");
done.addActionListener (this);
add (done);
}
public void actionPerformed (ActionEvent e)
{
if (p.getText ().equals ("computer"))
JOptionPane.showMessageDialog (null, "Welcome", "Welcome",
JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog (null, "Unauthorized Access", "Incorrect
Log on", JOptionPane.ERROR_MESSAGE);
p.setText ("");
}
}