- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we create a login form in Java?n
We can develop a login form in Java using Java Swing technology. In this example, we can create two labels username and password, two text fields for the user to enter valid credentials and finally one submit button. Once the user is able to enter the valid credentials in the two text fields, we can able to see Hello admin in the login form.
Example
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_label.setText("User Name :"); userName_text = new JTextField(); // Password Label password_label = new JLabel(); password_label.setText("Password :"); password_text = new JPasswordField(); // Submit submit = new JButton("SUBMIT"); 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); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Adding the listeners to components.. submit.addActionListener(this); add(panel, BorderLayout.CENTER); setTitle("Please Login Here !"); setSize(450,350); setVisible(true); } public static void main(String[] args) { new LoginDemo(); } @Override public void actionPerformed(ActionEvent ae) { String userName = userName_text.getText(); String password = password_text.getText(); if (userName.trim().equals("admin") && password.trim().equals("admin")) { message.setText(" Hello " + userName + ""); } else { message.setText(" Invalid user.. "); } } }
Output
- Related Articles
- How can we create a login form in Java?
- How to create a responsive login form with CSS?
- How to create a social media login form with CSS?
- How to create a responsive navigation menu with a login form inside it?
- How can we create a custom exception in Java?
- How can we create a JSON using JsonGenerator in Java?
- How can we create a Service Provider interface in Java 9?
- How can we create a JPopupMenu with a sub menu in Java?
- Can we create nested TitiledBorder in Java?
- How can we create an unmodifiable Set in Java 9?
- How can we create an unmodifiable List in Java 9?
- How can we create an unmodifiable Map in Java 9?
- How to create a responsive navigation menu with a login form inside of it with HTML and CSS?
- Can we create a program without a main method in Java?
- How can we create an instance of VarHandle in Java 9?
- How can we create a multi-release jar(mrjar) using jar tool in Java 9?

Advertisements