Professional Documents
Culture Documents
Practical no: 12
Code:
import javax.swing.*;
import java.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
args) {
JFrame("Password Field
Demo");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
JPasswordField passwordField
KeyListener()
public void
keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (c == '*') {
e.setKeyChar('#');
public void
keyPressed(KeyEvent e) {
public void
keyReleased(KeyEvent e)
});
frame.add(passwordField);
frame.setVisible(true);
Output:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
loginButton.addActionListener(new ActionListener() {
if (authenticate(username, password)) {
JOptionPane.showMessageDialog(frame, "Login Successful!");
} else {
JOptionPane.showMessageDialog(frame, "Login Failed. Please try
again.");
}
passwordField.setText("");
}
});
frame.add(usernameLabel);
frame.add(usernameField);
frame.add(passwordLabel);
frame.add(passwordField); frame.add(new
JLabel());
frame.add(loginButton);
frame.setVisible(true);
}
Output:
Code:
addButton.addActionListener(new ActionListener() {
frame.setVisible(true);
}
}
Output: