khirulnizam

LoginUI with Event Handling

Jul 30th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package kerul.net;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class LoginUI extends JFrame
  8.     implements ActionListener{
  9.    
  10.     private JTextField username;
  11.     private JPasswordField password;
  12.     private JButton btnlogin, btnreset;
  13.     //private Container pane;
  14.    
  15.     public LoginUI(){
  16.         username=new JTextField("Username",
  17.                 10);
  18.         password=new JPasswordField("Password",
  19.                 10);
  20.         btnlogin=new JButton ("Login");
  21.         btnlogin.addActionListener(this);
  22.         btnreset=new JButton ("Reset");
  23.         btnreset.addActionListener(this);
  24.        
  25.         //draw UIs
  26.         Container pane = getContentPane();
  27.         pane.setBackground(Color.BLUE);
  28.         pane.setLayout(new GridLayout(2,2));
  29.         pane.add(username);
  30.         pane.add(password);
  31.         pane.add(btnlogin);
  32.         pane.add(btnreset);
  33.     }//end constructor
  34.    
  35.     public void actionPerformed(ActionEvent e){
  36.         Object obj = e.getSource();
  37.         if (obj==btnlogin){
  38.             //capture data username
  39.             String u, p,msg;
  40.             u=username.getText().toString();
  41.             p=password.getText().toString();
  42.             msg="Hello "+u;
  43.             //default title and icon dialog box
  44.             JOptionPane.showMessageDialog(this,
  45.                 msg);
  46.         }
  47.         else if(obj==btnreset){
  48.             password.setText("");
  49.             username.setText("Your username here");
  50.             //username.setF
  51.         }
  52.     }//end actionPerformed
  53.  
  54.     public static void main(String[] args) {
  55.         LoginUI frame = new LoginUI();
  56.         frame.setDefaultCloseOperation(
  57.                 JFrame.EXIT_ON_CLOSE);
  58.         frame.setTitle("LoginUI");
  59.         frame.setSize(300, 300);
  60.         frame.setVisible(true);
  61.     }//end main
  62. }//end LoginUI
Advertisement
Add Comment
Please, Sign In to add comment