Tugas 7 PBO - Login Interface
Muhammad Izzuddin Al Islami
05111740000035
Deskripsi
Sebuah progam GUI (Graphical User Interface) dari buku Object First With Java. Program ini berupa form login dalam GUI Java dimana user dapat melakukan login dengan memasukkan kombinasi username dan password yang telah ditetapkan sebelumnya.
Class Diagram
Dekronstruksi Module/Class yang dibutuhkan
Aplikasi GUI ini memerlukan beberapa modul sebagai berikut:
- Javax.swing (Library Java untuk mengembangkan aplikasi GUI )
- Java.awt (Library GUI Java, menyediakan layout dan kelas-kelas graphical untuk Javax.swing)
- Java.awt.event (Library GUI Java untuk menghandle event dari aplikasi GUI)
- LoginUI (Class yang berisi method main, menghandle event login dan mengatur tampilan dari komponen GUI)
Implementasi (Source Code)
- LoginUI .java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginUI
{
String Username = "admin";
String Password = "123";
String msg = "";
private JTextField txtUsername;
private JPasswordField txtPassword;
public static void main (String args[])
{
LoginUI gui = new LoginUI();
gui.go();
}
public void go(){
JFrame frame = new JFrame("Login GUI demo App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
txtUsername = new JTextField(20);
txtPassword = new JPasswordField(20);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new LoginListener());
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new CancelListener());
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 class LoginListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if(Username.equals(txtUsername.getText())){
if(Password.equals(txtPassword.getText()))
{
msg = "Login Successfull!!";
}
else
{
msg = "Can't Login, Username/Password Incorrect!!";
}
}
else
{
msg = "Can't Login, Username/Password Incorrect!";
}
JOptionPane.showMessageDialog(null,msg);
}
}
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginUI
{
String Username = "admin";
String Password = "123";
String msg = "";
private JTextField txtUsername;
private JPasswordField txtPassword;
public static void main (String args[])
{
LoginUI gui = new LoginUI();
gui.go();
}
public void go(){
JFrame frame = new JFrame("Login GUI demo App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
txtUsername = new JTextField(20);
txtPassword = new JPasswordField(20);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new LoginListener());
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new CancelListener());
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 class LoginListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if(Username.equals(txtUsername.getText())){
if(Password.equals(txtPassword.getText()))
{
msg = "Login Successfull!!";
}
else
{
msg = "Can't Login, Username/Password Incorrect!!";
}
}
else
{
msg = "Can't Login, Username/Password Incorrect!";
}
JOptionPane.showMessageDialog(null,msg);
}
}
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
}
}
Comments
Post a Comment