0% found this document useful (0 votes)
5 views3 pages

GUI Login Example

The document provides a Java implementation of a simple GUI login system using Swing. It includes a Login class that prompts for a username and password, validating them against hardcoded credentials, and a Welcome class that displays a welcome message upon successful login. If the login fails, an error message is shown, and the input fields are cleared for re-entry.

Uploaded by

anjeru
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)
5 views3 pages

GUI Login Example

The document provides a Java implementation of a simple GUI login system using Swing. It includes a Login class that prompts for a username and password, validating them against hardcoded credentials, and a Welcome class that displays a welcome message upon successful login. If the login fails, an error message is shown, and the input fields are cleared for re-entry.

Uploaded by

anjeru
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

GUI Login Example

//Login Class on its own Java File


import [Link].*;
import [Link].*;
import [Link].*;
public class Login extends JFrame {
public static void main(String[] args) {
Login frame = new Login();
}
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
Login()
{
super("Login Authentication");
setSize(300,200);
setLocation(500,280);
[Link] (null);
[Link](70,30,150,20);
[Link](70,65,150,20);
[Link](110,100,80,20);
[Link](blogin); [Link](txuser); [Link](pass);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

1|Page
setVisible(true);
actionlogin();
}
//Method to check for login
public void actionlogin(){
[Link](new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String name = [Link]();
String password = [Link]();
//Checking whether the password keyed in is correct
if([Link]("test") && [Link]("12345")) {
//If Login is successful it calls the class Welcome
Welcome regFace =new Welcome();
[Link](true);
dispose();
}
else {
[Link](null,"Wrong Password / Username");
[Link]("");
[Link](""); [Link]();
}
}
});
}
}

2|Page
//Class Welcome in a different Java File
import [Link].*;
import [Link].*;
import [Link].*;
public class Welcome extends JFrame {
public static void main(String[] args) {
Welcome frame = new Welcome();
}
JLabel welcome = new JLabel("Welcome to MMU");
JPanel panel = new JPanel();
NewFrame(){
super("Welcome");
setSize(300,200);
setLocation(500,280);
[Link] (null);
[Link](70,50,150,60);
[Link](welcome);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

3|Page

You might also like