Created
December 8, 2017 02:09
-
-
Save Rahandi/f7a0d772cba4c25dcbc2f85b1ad404e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class Login{ | |
String Username = "rahandi"; | |
String Password = "rahandi"; | |
String msg = " "; | |
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(); | |
JLabel lblUsername = new JLabel("Username:"); | |
JLabel lblPassword = new JLabel("Password:"); | |
txtUsername = new JTextField(20); | |
txtPassword = new JTextField(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.getRootPane().setDefaultButton(btnLogin); | |
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 Granted"; | |
} | |
else{ | |
msg = "Login Denied"; | |
} | |
JOptionPane.showMessageDialog(null, msg); | |
} | |
} | |
} | |
public class CancelListener implements ActionListener{ | |
public void actionPerformed(ActionEvent event){ | |
txtUsername.setText(""); | |
txtPassword.setText(""); | |
txtUsername.requestFocus(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment