.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> OOPS >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Login form in swing

Posted By:laptop charles       Posted Date: July 15, 2010    Points: 10    Category: OOPS    URL: http://www.dotnetspark.com  

It is like a simple login form.
 

It contain two textbox and two label box and two button.
One button name is login and other one is cancel.
Compare the textbox and user name and password is equal .
Then login is success.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
class loginform1 extends JFrame implements ActionListener
{
JLabel jl1,jl2;
 JTextField jt1;
 JPasswordField jp1;
 JButton jb1,jb2,jb3;
 JLabel jpic;
 JLabel jlnew;
Container c;
 loginform1()
 {
   c=this.getContentPane();
    jl1=new JLabel("USER NAME");
    jl2=new JLabel("PASSWORD");
    jt1=new JTextField(10);
  try
	{
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	}
catch(Exception ex)
{
}
    jp1=new JPasswordField(10);
    jb1=new JButton("OK");
    jb2=new JButton("CANCEL");
    jb3=new JButton("EXIT ");
    c.setLayout(new BorderLayout());
    JLabel l1=new JLabel("student scholarship Management System",JLabel.CENTER);
    jpic=new JLabel();
    jpic.setIcon(new ImageIcon("images1/note.jpg"));

    JPanel sp=new JPanel();
    sp.setLayout(new BorderLayout());
    sp.add(new JLabel("            "),BorderLayout.NORTH);
    sp.add(new JLabel("            "),BorderLayout.EAST);
    sp.add(new JLabel("            "),BorderLayout.SOUTH);
    sp.add(new JLabel("            "),BorderLayout.WEST);
    EtchedBorder b=new EtchedBorder(Color.red,Color.blue);
    sp.setBorder(b);
    JPanel p3=new  JPanel(new GridLayout(1,2,1,1));
    p3.add(jpic);


   JPanel p1=new JPanel();
   p1.setLayout(new GridLayout(2,2,20,20));
  p1.setBorder(new  TitledBorder("Existing User"));
    p1.add(jl1);
    p1.add(jt1);
    p1.add(jl2);
    p1.add(jp1);
p3.add(p1);



    JPanel p2=new JPanel();
    p2.setLayout(new GridLayout(3,1,30,30));
      p2.add(jb1);
      p2.add(jb2);
       p2.add(jb3);
      jb1.addActionListener(this);
      jb2.addActionListener(this);
      c.add(l1,BorderLayout.NORTH);
      sp.add(p3);
      c.add(sp,BorderLayout.CENTER);
      c.add(new JLabel("           "),BorderLayout.WEST);
      c.add(p2,BorderLayout.EAST);
      c.add(new JLabel("           "),BorderLayout.SOUTH);
      setResizable(false);
      setLocation(100,200);
      setSize(550,200);
      jb3.addActionListener(this);
      setVisible(true);


  }

public void mouseReleased(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{

}



public void actionPerformed(ActionEvent ae)
{
  if (ae.getSource( )==jb1)
   {
          if(jt1.getText().equals("Admin") && jp1.getText().equals("ad"))
	{
	new mainmenu1();
        //jt1.setText("cijo");
	this.setVisible(false);
	}
          else 
          {
              JOptionPane.showMessageDialog(this,"User name (or) Password is wrong");
              
          }    
   }
  else if (ae.getSource( )==jb2)
  {
	jt1.setText("");
	jp1.setText("");
  }



 else if(ae.getSource()==jb3)
 {
            try
        {
	setVisible(false);


        }
        catch(Exception ex)
        {
   ex.printStackTrace();
        }

 }
}


 public static void main(String args[])
  {
     new loginform1();

  }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend