JAVA_PRACTICAL12
JAVA_PRACTICAL12
import java.awt.*;
import javax.swing.*;
f.setLayout(null);
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jpf.setEchoChar('#');
f.add(l2);
f.add(jtf);
f.add(jpf);
f.add(jb);
f.getContentPane().setBackground(Color.YELLOW);
f.setVisible(true);
}
}
Output:-
Exercise:-
Q.1) Write a program using JPasswordField and JTextField to demonstrate the use of user
authentication.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Authentication extends JFrame implements ActionListener {
JButton button;
JTextField jtf;
JPasswordField jpf;
public Authentication() {
setVisible(true);
setTitle("Authentication Frame");
setSize(300, 300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j2 = new JLabel("Password:");
add(jl);
add(jtf);
add(j2);
add(jpf);
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
s1 = jtf.getText();
s2 = new String(jpf.getPassword());
} else {
new Authentication(); }
}
Output:-
import java.awt.event.*;
import javax.swing.*;
JButton button;
public AdditionOfTwoNum() {
setVisible(true);
setTitle("Addition Frame");
setSize(300, 300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtf3.setEditable(false);
add(j1);
add(jtf1);
add(j2);
add(jtf2);
add(button);
add(jtf3);
button.addActionListener(this);
try {
int n1 = Integer.parseInt(jtf1.getText());
int n2 = Integer.parseInt(jtf2.getText());
int n3 = n1 + n2;
jtf3.setText(Integer.toString(n3));
new AdditionOfTwoNum();
}
}
Output:-
Q.3) Write a java program using JPasswordField l to accept password from the user if the
length is less than 6 characters, then error message should be displayed." Password length
must be greater than 6 characters”.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public PasswordCheck() {
setTitle("Password Checker");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
checkButton.addActionListener(new ActionListener() {
});
setLayout(new FlowLayout());
add(messageLabel);
add(passwordField);
add(checkButton);
if (password.length() < 6) {
} else {
} }
frame.setVisible(true);
}}
Output:-