15b
15b
Date: RollNo:
EXERCISE-15(b)
Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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);
}
@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