import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class PasswordField extends JFrame {
private JPanel passwordPanel;
private JPasswordField passwordField;
private JLabel passwordLabel;
private JButton userConfirmationBtn;
private JButton submenu;
private Image icon;
private ImageIcon frameBackground;
public PasswordField() {
icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
frameBackground = new ImageIcon("c:/pics/passwordbck2.jpg");
passwordField = new JPasswordField();
passwordLabel = new JLabel("Password: ");
userConfirmationBtn = new JButton("Enter");
submenu = new JButton("Submenu");
initComponents();
}
private void initComponents() {
// set the properties of this label
passwordLabel.setBounds(12, 13, 80, 20);
passwordLabel.setFont(new Font("Monospaced", Font.BOLD, 12));
passwordLabel.setForeground(Color.BLACK);
// set the properties and actions of this text field
passwordField.setBounds(88, 13, 185, 20);
// set the properties and actions of this button
Action pass = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// retrive the password data from the password field
char[] passwordChars = passwordField.getPassword();
String passwordStr = "";
// convert the returned password characters as string
for (int x = 0; x <= passwordChars.length - 1; x++) {
passwordStr = passwordStr + passwordChars[x];
}
// check if the password entered is valid
if (Password.isPasswordAuthentic(passwordStr)) {
// do something here
}
else if (passwordStr.equals("")) {
// do something here
}
else {
// do something here
}
}
};
userConfirmationBtn.addActionListener(pass);
userConfirmationBtn.setMnemonic(KeyEvent.VK_E);
userConfirmationBtn.setBounds(88, 55, 90, 20);
userConfirmationBtn.setFont(new Font("Monospaced", Font.BOLD, 11));
userConfirmationBtn.setBackground(Color.LIGHT_GRAY);
userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
JComponent.WHEN_IN_FOCUSED_WINDOW);
userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
JComponent.WHEN_IN_FOCUSED_WINDOW);
passwordPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
// Scale image to size of component
Dimension d = getSize();
g.drawImage(frameBackground.getImage(), 0, 0, d.width, d.height, null);
super.paintComponent(g);
}
};
passwordPanel.setOpaque(false);
// set this panel's layout to null for absolute positioning of
// components
passwordPanel.setLayout(null);
// add the components to this panel
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
passwordPanel.add(userConfirmationBtn);
// add the panel to the container
getContentPane().add(passwordPanel);
setTitle(" User Confirmation");
setIconImage(icon);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(288, 120);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
/**
* Invoke this method if this window doesnt have any more use for this
* current event.
*/
private void disableWindow() {
setVisible(false);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PasswordField();
}
});
}
}