Membuat Form Login Sederhana

Contoh program sederhana yang menampilkan form login di dalam JFrame. Inputan username berupa JTextField dan inputan password berupa JPasswordField serta tombol dibuat dengan JButton. Form sederhana ini juga mendemonstrasikan adanya Listener berupa ActionListener.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleLogin extends JFrame implements ActionListener {
	private JLabel label1, label2;
	private JTextField txtUser;
	private JPasswordField pwdPass;
	private JButton btnLogin, btnExit;
	
	public SimpleLogin() {
		super ("Login here...");
		
		Container container = getContentPane();
		container.setLayout(new FlowLayout());
		
		label1 = new JLabel ("Username : ");
		label2 = new JLabel ("Password : ");
		
		txtUser = new JTextField (20);
		txtUser.setToolTipText("Input Username");
		pwdPass = new JPasswordField(20);
		
		btnLogin = new JButton ("Login");
		btnLogin.addActionListener(this);
		btnExit = new JButton ("Exit");
		btnExit.addActionListener(this);
		
		container.add(label1);
		container.add(txtUser);
		container.add(label2);
		container.add(pwdPass);
		container.add(btnLogin);
		container.add(btnExit);
		
		setSize (300,200);
		setVisible (true);
	}
	
	public static void main (String args[]) {
		SimpleLogin test = new SimpleLogin();
		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private String user = "", pass = "";
	public void actionPerformed (ActionEvent e) {
		if (e.getSource() == btnLogin) {
			user = txtUser.getText();
			pass = pwdPass.getText();
			if (user.equals("achmatim") && pass.equals("otim")) {
				JOptionPane.showMessageDialog(null, "Login successfull");
			} else {
				JOptionPane.showMessageDialog(null, "Username and password dosn't match!");
				txtUser.setText("");
				pwdPass.setText("");
				txtUser.requestFocus(true);
			}
		} else if (e.getSource() == btnExit){
			JOptionPane.showMessageDialog(null,"Thanks to try my program. See you..");
			System.exit(0);
		}
	}
}

Screen shoot

0 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 5 (0 votes, average: 0.00 out of 5)
You need to be a registered member to rate this post.
Loading ... Loading ...
Java, Java Swing

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

40 Responses to “Membuat Form Login Sederhana”

Leave Comment

(required)

(required)


// Place This Code At The End Or After All Ad Zone Div Created