Header Ads

java - Creating a simple login form


Add images to project !
 1 : right click to your project ->new - > folder
and set name "Images" or whatever you want :P

2: copy your image and paste to that folder .(folder you was create) .






LogInGui.java


package thanhcs.login;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
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;
import javax.swing.SwingConstants;

public class LogInGui extends JFrame implements ActionListener{
    
    private JTextField txtUserName;
    private JPasswordField txtPSW;
    private JButton btnLogin, btnCancel;
    
    public LogInGui()
    {
        /**
         * Create jpanel
         * */
        JPanel jpTop = new JPanel();
        JPanel jpCenter = new JPanel(new BorderLayout());
        JPanel jpBot = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
        
        /**
         * add jpanel to jFrame with position 
         * */
        add(jpTop, BorderLayout.NORTH);
        add(jpCenter, BorderLayout.CENTER);
        add(jpBot, BorderLayout.SOUTH);
        /**
         * add border
         * */
        jpTop.setBorder(BorderFactory.createLineBorder(Color.CYAN));
        jpCenter.setBorder(BorderFactory.createLineBorder(Color.RED));
        jpBot.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
        
        
        /***
         * Create and add logo on jpTop
         * */
        JLabel jlb1 = new JLabel("ThanhCS94", SwingConstants.CENTER);
        jlb1.setForeground(Color.BLACK);
        jpTop.add(jlb1);
        
        /**
         * Create and add button to Bottom //Logon and exit
         * */
        btnLogin = new JButton("Logon", new ImageIcon("Image/on.png"));
        btnCancel = new JButton("Cancel", new ImageIcon("Image/off.png"));
        jpBot.add(btnLogin);
        jpBot.add(btnCancel);
        
        /**
         * Create image fiel and user, pass in jpCenter
         * */
        
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        jp1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        jp2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        
        jp1.add(new JLabel(new ImageIcon("Image/logon.png"), SwingConstants.CENTER));
        jpCenter.add(jp1, BorderLayout.WEST);
        
    
        
//        jp2.setLayout(new GridLayout(2, 2));
        
        JPanel jp11 = new JPanel();
        JPanel jp21 = new JPanel();
        
        jp11.add(new JLabel("User Name : "));
        jp11.add(txtUserName = new JTextField(10));

        jp21.add(new JLabel("Password : "));
        jp21.add(txtPSW = new JPasswordField(10));
        Box b = Box.createVerticalBox();
        b.add(Box.createVerticalStrut(20));
        b.add(jp11); b.add(jp21);
        jpCenter.add(b, BorderLayout.CENTER);
        
        
        btnCancel.addActionListener(this);
        btnLogin.addActionListener(this);
        
        /***/
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(420, 290);
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object ob = e.getSource();
        if(ob.equals(btnLogin))
        {
            //default user : ThanhCS94  , pass : 1234567788
            if(txtUserName.getText().trim().equals("ThanhCS94")&& txtPSW.getText().trim().equals("123456788"))
            {
                JOptionPane.showMessageDialog(null, "Success");
            }
            else
                if(txtUserName.getText().trim().equals("")|| txtPSW.getText().trim().equals(""))
                {
                    JOptionPane.showMessageDialog(null, "You must fill in all of the Fields");
                }
            else
            {
                JOptionPane.showMessageDialog(null, "Incorrect user or password,please check again ?");
                txtPSW.setText("");
                txtUserName.setText("");
            }
        }
        
        if(ob.equals(btnCancel))
        {
            int selValue = JOptionPane.showConfirmDialog(this, "Are you sure to exit program?", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(selValue == JOptionPane.YES_OPTION)
            System.exit(0);
        }
    }
   
    

    //main 
    public static void main(String[] args) {
        
        new LogInGui();
    }
    

}

No comments:

Powered by Blogger.