This is a Java program to create 2 labels and text fields for entering name and secured password.
We have to write a program in Java such that it creates a secured login field containing a name and secured password. When the user clicks view button, the username and password will be displayed.
For creating a secured login field, we can have the following set of input and output.
To display the username and password :
If the name 'thomas' and password 'thomas@123' are typed in the respective field, the expected output is "Username = thomas Password = thomas@123"
1. Create a frame, two labels for ‘Name’ and ‘Password’, a text field for name, a password field for password, a text field for output and a button to view the output.
2. Position the fields and button properly on the frame.
3. Associate ActionListener with the button.
4. When the button is clicked, display the username and password in the output text field.
Here is source code of the Java Program to create a secured login field. The program is successfully compiled and tested using javac compiler on Fedora 30.
/*Java Program to create a secured login field*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Login implements ActionListener
{
static JTextField name;
static JPasswordField pswd;
static JButton view;
static JTextField text;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("Login");
frame.setSize(400,400);
frame.setLayout(null);
frame.setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create two labels
JLabel l_name=new JLabel("Name :");
JLabel l_pswd=new JLabel("Password :");
l_name.setBounds(50,50,50,50);
l_pswd.setBounds(20,100,80,50);
frame.add(l_name);
frame.add(l_pswd);
//Create two text fields for name and password
name=new JTextField();
pswd=new JPasswordField();
name.setBounds(100,50,250,50);
pswd.setBounds(100,100,250,50);
frame.add(name);
frame.add(pswd);
//Create a button
view=new JButton("View");
view.setBounds(150,200,100,50);
frame.add(view);
//Create a text field to display the username and password
text=new JTextField();
text.setBounds(0,250,400,50);
frame.add(text);
//Create an object
Login obj=new Login();
//Associate ActionListener with the button
view.addActionListener(obj);
//Display the frame
frame.setVisible(true);
}
//function to display the username and password typed
public void actionPerformed(ActionEvent e)
{
String pass=String.valueOf(pswd.getPassword());
text.setText("Username = "+name.getText());
text.setText(text.getText()+" Password = "+pass);
}
}
1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(400,400); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create 2 labels “Name” and “Password” by using JLabel l_name=new JLabel(“Name :”); and JLabel l_pswd=new JLabel(“Password :”);. Once label is created add it to the frame by using frame.add();.
4. By using setBounds() values we can set the position of label names.
5. Create two text fields for name and password with setBounds(int x, int y, int width, int height), where ‘x’ & ‘y’ is used to set the position of top-left corner. ‘int width’ & ‘int height’ is used to set the width and height of the exit button. Once fields are created add them to the frame by using frame.add();.
name=new JTextField(); –> Creates a textfield for name.
pswd=new JPasswordField(); –> Creates a password field for secured password.
name.setBounds(100,50,250,50); –> Sets the position of name textfield.
pswd.setBounds(100,100,250,50); –> Sets the position of password field.
6. Create a view button with a setBounds() values and add it to the frame by using frame.add();.
view=new JButton(“View”); –> Used to create the view button.
view.setBounds(150,200,100,50); –> Sets the position of button.
frame.add(view); –> It will add the button to the frame.
7. Create one more text field for output to display the username and password.
8. Login obj=new Login(); is used to create the object. Once object is created associate ActionListener with the button.
9. frame.setVisible(true); is used to display the frame.
10. String pass=String.valueOf(pswd.getPassword()); –> getPassword returns a char[] of the string typed in the password field. It is converted to String format by using function String.valueOf.
11. When the View button is clicked, it display the username and password in the output text field.
text.setText(“Username = “+name.getText()); –> It displays the name in output text field
text.setText(text.getText()+” Password = “+pass); –> It displays the password in output text field.
Here’s the run time test case for getting username and password from a secured login field.
Test case – Here’s the runtime output to get the username and password. For example, if user enters Name as “thomas” and Password as “thomas@123” in the text field. After clicking view button it should display “Username = thomas Password = thomas@123”.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Programming Books
- Practice BCA MCQs
- Apply for Java Internship
- Check Java Books
- Practice Programming MCQs