Create login form which contains a userid, password field, and two buttons, Submit and Reset. If the userid or password field is left blank, then on click of submit button, show a message to the user to fill in the fields. On click of reset button, clear the fields.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT,RESET;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
RESET=new JButton("RESET");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
panel.add(RESET);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
RESET.addActionListener(this);
setTitle("LOGIN FORM by KAUSHAL");
}
public void actionPerformed(ActionEvent ae)
{
JButton b = (JButton) ae.getSource();
if (b == SUBMIT)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("") || value2.equals(""))
{
JOptionPane.showMessageDialog(this,"username and password must not blank",
"Error",JOptionPane.ERROR_MESSAGE);}
else
{
JOptionPane.showMessageDialog(this,"welcome"+" "+value1);
}
}
else
{
text1.setText("");
text2.setText("");
}
}
}
class pract3
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
Posted By Kaushal Prajapti

Make Comments..!!


Sarang Vandara
At Line-9 text2 should be declared as a JPasswordField instead of JTextField..
Like · Comment ·
Joydip Panchal likes this
Kaushal Prajapti
JPasswordField is correct for values like **** as problem statment tell us that we have to create password field thankyou
Like · 2 ·
Sarang Vandara
GridLayout Should be (3,2) instead of (3,1)
Like · Comment ·
Joydip Panchal likes this
Download Android App