0% found this document useful (0 votes)
16 views

15b

Uploaded by

manvithsai1001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

15b

Uploaded by

manvithsai1001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise:

Date: RollNo:

EXERCISE-15(b)

15(b).Aim:-Write a JAVA program to create a Login Form using swing Components

Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginDemo extends JFrame implements ActionListener {


JPanel panel;
JLabel user_label, password_label, message;
JTextField userName_text;
JPasswordField password_text;
JButton submit, cancel;

LoginDemo() {
// Username Label
user_label = new JLabel("User Name:");
userName_text = new JTextField();

// Password Label
password_label = new JLabel("Password:");
password_text = new JPasswordField();

// Submit Button
submit = new JButton("SUBMIT");

// Panel
panel = new JPanel(new GridLayout(3, 1));
panel.add(user_label);
panel.add(userName_text);
panel.add(password_label);
panel.add(password_text);
message = new JLabel();
panel.add(message);
panel.add(submit);

// Set Frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel, BorderLayout.CENTER);
setTitle("Please Login Here!");
setSize(450, 350);
setVisible(true);

KALLAMHARANADHAREDDYINSTITUTEOFTECHNOLOGY,CHOWDAVARAM
GUNTUR
Exercise:
Date: RollNo:

// Adding ActionListener
submit.addActionListener(this);
}

public static void main(String[] args) {


new LoginDemo();
}

@Override
public void actionPerformed(ActionEvent ae) {
String userName = userName_text.getText();
String password = new String(password_text.getPassword());
if (userName.trim().equals("admin") && password.trim().equals("admin")) {
message.setText("Hello " + userName);
} else {
message.setText("Invalid user..");
}
}
}

KALLAMHARANADHAREDDYINSTITUTEOFTECHNOLOGY,CHOWDAVARAM
GUNTUR

You might also like