Advertisement
Guest User

class LoginDialog

a guest
May 10th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. package currencyconvertergui;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6.  
  7. public class LoginDialog extends JDialog{
  8.    
  9.     private JTextField tfUsername;
  10.     private JPasswordField pfPassword;
  11.     private JLabel lbUsername;
  12.     private JLabel lbPassword;
  13.     private JButton btnLogin;
  14.     private JButton btnCancel;
  15.     private boolean succeeded;
  16.    
  17.     public LoginDialog(Frame parent) {
  18.         super(parent, "Login", true);
  19.         //
  20.         JPanel panel = new JPanel(new GridBagLayout());
  21.         GridBagConstraints cs = new GridBagConstraints();
  22.  
  23.         cs.fill = GridBagConstraints.HORIZONTAL;
  24.  
  25.         lbUsername = new JLabel("Username: ");
  26.         cs.gridx = 0;
  27.         cs.gridy = 0;
  28.         cs.gridwidth = 1;
  29.         panel.add(lbUsername, cs);
  30.  
  31.         tfUsername = new JTextField(20);
  32.         cs.gridx = 1;
  33.         cs.gridy = 0;
  34.         cs.gridwidth = 2;
  35.         panel.add(tfUsername, cs);
  36.  
  37.         lbPassword = new JLabel("Password: ");
  38.         cs.gridx = 0;
  39.         cs.gridy = 1;
  40.         cs.gridwidth = 1;
  41.         panel.add(lbPassword, cs);
  42.  
  43.         pfPassword = new JPasswordField(20);
  44.         cs.gridx = 1;
  45.         cs.gridy = 1;
  46.         cs.gridwidth = 2;
  47.         panel.add(pfPassword, cs);
  48.         panel.setBorder(new LineBorder(Color.GRAY));
  49.  
  50.         btnLogin = new JButton("Login");
  51.  
  52.         btnLogin.addActionListener(new ActionListener() {
  53.  
  54.             public void actionPerformed(ActionEvent e) {
  55.                 if (Login.authenticate(getUsername(), getPassword())) {
  56.                     JOptionPane.showMessageDialog(LoginDialog.this,
  57.                             "Hi " + getUsername() + "! Welcome to Soley Money Changer.",
  58.                             "Login",
  59.                             JOptionPane.INFORMATION_MESSAGE);
  60.                     succeeded = true;
  61.                     dispose();
  62.                 } else {
  63.                     JOptionPane.showMessageDialog(LoginDialog.this,
  64.                             "Invalid username or password",
  65.                             "Login",
  66.                             JOptionPane.ERROR_MESSAGE);
  67.                     // reset username and password
  68.                     tfUsername.setText("");
  69.                     pfPassword.setText("");
  70.                     succeeded = false;
  71.  
  72.                 }
  73.             }
  74.         });
  75.         btnCancel = new JButton("Cancel");
  76.         btnCancel.addActionListener(new ActionListener() {
  77.  
  78.             public void actionPerformed(ActionEvent e) {
  79.                 dispose();
  80.             }
  81.         });
  82.         JPanel bp = new JPanel();
  83.         bp.add(btnLogin);
  84.         bp.add(btnCancel);
  85.  
  86.         getContentPane().add(panel, BorderLayout.CENTER);
  87.         getContentPane().add(bp, BorderLayout.PAGE_END);
  88.  
  89.         pack();
  90.         setResizable(false);
  91.         setLocationRelativeTo(parent);
  92.     }
  93.      
  94.     public String getUsername() {
  95.         return tfUsername.getText().trim();
  96.     }
  97.  
  98.     public String getPassword() {
  99.         return new String(pfPassword.getPassword());
  100.     }
  101.  
  102.     public boolean isSucceeded() {
  103.         return succeeded;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement