Login Panel GUI
Nama : Muhammad Fawwaz Zuhdan Nauvali
NRP : 05111740000106
Kelas : Pemrograman Berbasis Objek AMembuat Login Panel dengan GUI dalam java di BlueJ
Source Code :
Login.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login{
JTextField txtUsername = null;
JTextField txtPassword = null;
public static void main(String[] args){
Login gui = new Login();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
txtUsername = new JTextField(20);
txtPassword = new JPasswordField(20);
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new LoginListener(this));
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new CancelListener(this));
panel.add(lblUsername);
panel.add(txtUsername);
panel.add(lblPassword);
panel.add(txtPassword);
panel.add(btnLogin);
panel.add(btnCancel);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void sendMessage(String msg) {
JOptionPane.showMessageDialog(null,msg);
}
public String getUsername() {
return txtUsername.getText();
}
public String getPassword() {
return txtPassword.getText();
}
public void clear() {
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
public class LoginListener implements ActionListener{
String username = "fawwaz";
String password = "fawwaz";
Login login;
public LoginListener(Login login) {
this.login = login;
}
public void actionPerformed(ActionEvent event){
if(username.equals(login.getUsername()) && password.equals(login.getPassword())){
login.sendMessage("Welcome "+login.getUsername());
}else{
login.sendMessage("Login Failed");
}
}
}
public class CancelListener implements ActionListener{
Login login;
public CancelListener(Login login) {
this.login = login;
}
public void actionPerformed(ActionEvent event){
login.clear();
}
}
}
Hasil Output Program
Hasil Output ketika username atau password salah
Hasil Output ketika username atau password benar
Komentar
Posting Komentar