SWING LOGIN FORM DI JAVA
Membuat
form login java menggunakan class swing NetBeans IDE dan menggunakan database
mysql serta koneksi kedatabasenya menggunakan jdbc.Buatlah project baru di
netbeans ide, project java > java aplications dan beri nama
"aplikasilogin.java", jika sudah selesai lanjut kita akan membuat
sebuah database menggunakan mysql.
MEMBUAT DATABASE LOGIN SEDERHANA
Pada
netbeans ide, jendela services pilih database > dan klik kanan MYSQL Servers
> create new database, buatlah nama database kamu "aplikasilogin"
dan ok. selanjutnya membuat tabel dan kolom database "aplikasilogin".
pada connection "jdbc:mysql://localhost:3306/aplikasilogin?zeroDateTimeBehavior=convertToNull
[root on Default schema]" pilih nama database "aplikasilogin"
dan klik kanan pada table > create table, isikan nama tabel dengan nama
"user". selanjutnya add column. isiskan nama colomn
"id,username,password" untuk detail pembuatan tabel dan kolom
silahkan lihat gambar berikut :
Selesai membuat database MySQL kembali ke jendela project java pada NetBeans IDE.
Membuat
Form Login Java
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package aplikasilogin;
/**
*
* @author b0x
*/
public
class NewClass
{
public
static boolean authenticate(String
username, String password) {
// hardcoded
username and password
if (username.equals("sectorcode")
&& password.equals("ganteng")) {
return true;
}
return false;
}
}
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package aplikasilogin;
/**
*
* @author b0x
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class loginclass extends
JDialog {
private JTextField
tfUsername;
private JPasswordField
pfPassword;
private JLabel
lbUsername;
private JLabel
lbPassword;
private JButton
btnLogin;
private JButton
btnCancel;
private boolean succeeded;
public
loginclass(Frame
parent) {
super(parent,
"Login", true);
//
JPanel panel
= new JPanel(new GridBagLayout());
GridBagConstraints
cs = new GridBagConstraints();
cs.fill =
GridBagConstraints.HORIZONTAL;
lbUsername =
new JLabel("Username: ");
cs.gridx =
0;
cs.gridy =
0;
cs.gridwidth
= 1;
panel.add(lbUsername,
cs);
tfUsername
= new JTextField(20);
cs.gridx
= 1;
cs.gridy
= 0;
cs.gridwidth
= 2;
panel.add(tfUsername,
cs);
lbPassword
= new JLabel("Password: ");
cs.gridx
= 0;
cs.gridy
= 1;
cs.gridwidth
= 1;
panel.add(lbPassword,
cs);
pfPassword
= new JPasswordField(20);
cs.gridx
= 1;
cs.gridy
= 1;
cs.gridwidth
= 2;
panel.add(pfPassword,
cs);
panel.setBorder(new LineBorder(Color.GRAY));
btnLogin
= new JButton("Login");
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent
e) {
if (Login.authenticate(getUsername(),
getPassword())) {
JOptionPane.showMessageDialog(LoginDialog.this,
"Hi
" + getUsername() + "! You have successfully logged
in.",
"Login",
JOptionPane.INFORMATION_MESSAGE);
succeeded
= true;
dispose();
}
else {
JOptionPane.showMessageDialog(LoginDialog.this,
"Invalid
username or password",
"Login",
JOptionPane.ERROR_MESSAGE);
//
reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded
= false;
}
}
});
btnCancel
= new JButton("Cancel");
btnCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent
e) {
dispose();
}
});
JPanel
bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);
getContentPane().add(panel,
BorderLayout.CENTER);
getContentPane().add(bp,
BorderLayout.PAGE_END);
pack();
setResizable(false);
setLocationRelativeTo(parent);
}
public
String getUsername() {
return tfUsername.getText().trim();
}
public
String getPassword() {
return new String(pfPassword.getPassword());
}
public
boolean isSucceeded()
{
return succeeded;
}
}
/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package aplikasilogin;
/**
*
* @author b0x
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public
static void main(String[]
args) {
final JFrame
frame = new JFrame("JDialog Demo");
final JButton
btnLogin = new JButton("Click to login");
btnLogin.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent
e) {
LoginDialog
loginDlg = new LoginDialog(frame);
loginDlg.setVisible(true);
//
if logon successfully
if(loginDlg.isSucceeded()){
btnLogin.setText("Hi
" + loginDlg.getUsername() + "!");
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,
100);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(btnLogin);
frame.setVisible(true);
}
}
No comments:
Post a Comment