Advertisement
enkacang

ipt lbe3

Oct 31st, 2022
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. //01DDT20F1122
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class LabTask3 extends JFrame {
  8.    
  9.     private JPasswordField _password;
  10.     private JComboBox _user;
  11.     private JCheckBox _showPassword;
  12.     private JButton _buttonLogin;
  13.     private JButton _buttonCancel;
  14.    
  15.     private JPanel _mainPanel;
  16.    
  17.     private String[] _userNameList = { "Zainal Abidin", "Moktar Dahari", "Fernando Forestieri", "John Jhonny Jonarthan", "Pele" };
  18.     private String[] _userPasswordList = { "12345", "5555", "12435134", "423412", "1234156" };
  19.  
  20.     public LabTask3() {
  21.        
  22.         _mainPanel = new JPanel();
  23.         _mainPanel.setLayout(new BoxLayout(_mainPanel, BoxLayout.Y_AXIS));
  24.        
  25.         this.createNewImageLabel("C:\\Users\\User\\Downloads\\profile1.png");
  26.         _user = this.createNewComboBox("Username:",_userNameList);
  27.         _password = this.createNewTextFieldForm("Password:", 10);
  28.         _showPassword = this.createNewJCheckBox("Show Password", "                   ");
  29.        
  30.         String[] buttonName = {"Login", "Cancel"};
  31.         JButton[] _buttonCollection = this.createNewJButton(buttonName);
  32.        
  33.         _buttonCollection[0].addActionListener(new ActionListener() {
  34.  
  35.             public void actionPerformed(ActionEvent evt) {
  36.                
  37.                 if(_userPasswordList[_user.getSelectedIndex()].equals(_password.getText()))
  38.                 {
  39.                     JOptionPane.showMessageDialog(new JFrame(), "SUCCESFULL LOGIN", "SUCCESSS!!", 1);
  40.                 }
  41.                 else
  42.                 {
  43.                     JOptionPane.showMessageDialog(new JFrame(), "WRONG PASSWORD", "ERROR!!", 2);
  44.                 }
  45.                
  46.             }
  47.         });
  48.        
  49.         _buttonCollection[1].addActionListener(new ActionListener() {
  50.  
  51.             public void actionPerformed(ActionEvent evt) {
  52.                
  53.                 System.exit(0);
  54.                
  55.             }
  56.            
  57.         });
  58.        
  59.         _showPassword.addItemListener(new ItemListener() {
  60.             @Override
  61.             public void itemStateChanged(ItemEvent e) {
  62.                 if (e.getStateChange() == ItemEvent.SELECTED) {
  63.                     System.out.println("SELECTED");
  64.                     _password.setEchoChar((char) 0);
  65.  
  66.                 } else {
  67.                     System.out.println("DESELECTED");
  68.                    
  69.                     _password.setEchoChar('*');
  70.                 }
  71.             }
  72.         });
  73.        
  74.         this.iniTialization();
  75.     }
  76.      
  77.     private void iniTialization()
  78.     {
  79.         this.add(_mainPanel);
  80.         this.setTitle("LabTask3");
  81.         this.setDefaultCloseOperation(3);
  82.         this.setVisible(true);
  83.         this.setLayout(new FlowLayout());
  84.         this.setSize(500,500); 
  85.     }
  86.    
  87.     private void createNewImageLabel(String path)
  88.     {
  89.         JPanel tempPanel = new JPanel(new FlowLayout() );
  90.         JLabel image = new JLabel(new ImageIcon(path));
  91.         tempPanel.add(image);
  92.         _mainPanel.add(tempPanel);
  93.     }
  94.    
  95.     private JPasswordField createNewTextFieldForm(String label, int textFieldsSize)
  96.     {
  97.         JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT) );
  98.        
  99.         JPasswordField tempTextField = new JPasswordField(textFieldsSize);
  100.         tempPanel.add(new JLabel(label));
  101.         tempPanel.add(tempTextField);
  102.        
  103.         _mainPanel.add(tempPanel);
  104.        
  105.         return tempTextField;
  106.     }
  107.    
  108.     private JComboBox createNewComboBox(String label, String[] userList)
  109.     {
  110.         JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT) );
  111.         JComboBox tempComboBox = new JComboBox(userList);
  112.        
  113.        
  114.         tempPanel.add(new JLabel(label));
  115.         tempPanel.add(tempComboBox);
  116.         _mainPanel.add(tempPanel);
  117.        
  118.         return tempComboBox;
  119.     }
  120.    
  121.     private JCheckBox createNewJCheckBox(String label, String spacer)
  122.     {
  123.         JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  124.         JCheckBox tempComboBox = new JCheckBox(label);
  125.        
  126.        
  127.         tempPanel.add(new JLabel(spacer));
  128.         tempPanel.add(tempComboBox);
  129.         _mainPanel.add(tempPanel);
  130.        
  131.         return tempComboBox;
  132.     }
  133.    
  134.     private JButton[] createNewJButton(String[] button)
  135.     {
  136.         JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  137.         JButton button1 = new JButton(button[0]);
  138.         JButton button2 = new JButton(button[1]);
  139.         button1.setPreferredSize(new Dimension(100, 40));
  140.         button2.setPreferredSize(new Dimension(100, 40));
  141.        
  142.         tempPanel.add(button1);
  143.         tempPanel.add(button2);
  144.         _mainPanel.add(tempPanel);
  145.        
  146.         return new JButton[] {button1, button2};
  147.     }    
  148.                
  149.     public static void main(String args[])
  150.     {
  151.         new LabTask3();
  152.     }
  153.    
  154.    
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement