The following code may help you
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener{
JTextField t1_uname;
JPasswordField t2_pswd;
JButton btn_lgn,btn_clr;
public Login()
{
setLayout(null);
setTitle("LOGIN");
setSize(830,720);
setLocation(300,8);
JPanel p1=new JPanel();
p1.setLayout(null);
p1.setBounds(0,0,830,720);
p1.setBackground(Color.white);
add(p1);
JLabel l1=new JLabel("USERNAME :");
l1.setBounds(140,230,130,20);
p1.add(l1);
t1_uname=new JTextField();
t1_uname.setBounds(250,230,130,20);
p1.add(t1_uname);
JLabel l2=new JLabel("PASSWORD :");
l2.setBounds(140,270,130,20);
p1.add(l2);
t2_pswd=new JPasswordField();
t2_pswd.setBounds(250,270,130,20);
p1.add(t2_pswd);
btn_lgn=new JButton("LOGIN");
btn_lgn.setBounds(250,310,100,20);
btn_lgn.addActionListener(this);
p1.add(btn_lgn);
btn_clr=new JButton("CLEAR");
btn_clr.setBounds(360,310,100,20);
btn_clr.addActionListener(this);
p1.add(btn_clr);
JLabel background=new JLabel(new ImageIcon("images/hd_login.jpg"));
background.setBounds(130,80,830,720);
p1.add(background);
setVisible(true);
}
public static void main(String[] args) {
Login obj=new Login();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn_lgn)
{
String uname=t1_uname.getText();
String pswd=t2_pswd.getText();
Dbconnection obj=new Dbconnection();
int k=obj.loginCheck(uname, pswd);
if(k==1)
{
AdminHome obj_AdminHome=new AdminHome();
setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null,"WRONG USERNAMR OR PASSWORD");
}
}
else if(e.getSource()==btn_clr)
{
t1_uname.setText("");
t2_pswd.setText("");
}
}
}
Method in DbConnection, this is just a method , you have to create connection to database,
import java.sql.*;
import java.util.*;
public class Dbconnection {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
public Dbconnection()
{
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME","root","root");
}
catch(Exception e)
{
System.out.println("Error in connection"+e);
}
}
public int loginCheck(String username,String password)
{
int flag=0;
try{
ps=con.prepareStatement("select * from tbl_login where username=? and password=? ");
ps.setString(1,username);
ps.setString(2,password);
rs=ps.executeQuery();
while(rs.next())
{
flag=1;
}
}
catch(Exception e)
{
System.out.println("Error in loginCheck"+e);
}
return flag;
}
}
Here, AdminHome page is new JFrame, that you have to create, the all components created in CONSTRUCTOR of the AdminHome class, So, creating the object of that class will show the JFrame and its components
Dbconnection is a class that has the connection to database and method for login check