Advertisement
silentkiler029

Azhar_Question-02_Registration_Form

Sep 29th, 2021
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.87 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import javax.swing.border.Border;
  5. import java.awt.event.*;
  6.  
  7. public class problem2 extends JFrame implements ActionListener {
  8.  
  9.     private Container c;
  10.     private Font font;
  11.     private JButton clr, val, regs;
  12.     private JLabel reg, name, bday, gender, email, pw, repw, stts;
  13.     private JRadioButton male, female, other;
  14.     private ButtonGroup bg;
  15.     private JTextField namef, emailf, pwf, repwf;
  16.     private JComboBox<String> cb1, cb2, cb3;
  17.  
  18.     String day[] = new String[32];
  19.     String year[] = new String[151];
  20.     String month[] = {"month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  21.  
  22.     problem2() {
  23.         setSize( 1100, 550 );
  24.         setLocationRelativeTo(null);
  25.         setTitle("Registration");
  26.         setResizable(true);
  27.  
  28.         init();
  29.     }
  30.  
  31.     public void init() {
  32.         // contentpane
  33.         c = this.getContentPane();
  34.         c.setLayout(null);
  35.  
  36.         font = new Font("Arial", Font.BOLD, 14);
  37.         addJLabel();
  38.         addTextBox();
  39.         addButton();
  40.         addComboBox();
  41.         addRadioButton();
  42.     }
  43.  
  44.     void addJLabel() {
  45.         Border border = BorderFactory.createLineBorder(Color.black);
  46.         // JLabel
  47.         reg = new JLabel("Registration");
  48.         name = new JLabel("Name");
  49.         bday = new JLabel("Birthday");
  50.         gender = new JLabel("Gender");
  51.         email = new JLabel("Email");
  52.         pw = new JLabel("Password");
  53.         repw = new JLabel("Retype");
  54.         stts = new JLabel();
  55.        
  56.         reg.setBounds(0, 0, 1100, 50);
  57.         reg.setFont(font);
  58.         reg.setHorizontalAlignment(JLabel.CENTER);
  59.         reg.setBorder(border);
  60.         c.add(reg);
  61.  
  62.        
  63.         name.setBounds(50, 100, 150, 30);
  64.         name.setFont(font);
  65.         c.add(name);
  66.  
  67.        
  68.         bday.setBounds(50, 150, 150, 30);
  69.         bday.setFont(font);
  70.         c.add(bday);
  71.  
  72.        
  73.         gender.setBounds(50, 200, 150, 30);
  74.         gender.setFont(font);
  75.         c.add(gender);
  76.  
  77.        
  78.         email.setBounds(50, 250, 150, 30);
  79.         email.setFont(font);
  80.         c.add(email);
  81.  
  82.        
  83.         pw.setBounds(50, 300, 150, 30);
  84.         pw.setFont(font);
  85.         c.add(pw);
  86.  
  87.        
  88.         repw.setBounds(500, 300, 150, 30);
  89.         repw.setFont(font);
  90.         c.add(repw);
  91.  
  92.         stts.setText("Status: ");
  93.         stts.setBounds(50, 450, 1050, 30);
  94.         stts.setFont(font);
  95.         c.add(stts);
  96.     }
  97.  
  98.     void addTextBox()
  99.     {
  100.         Border border = BorderFactory.createLineBorder(Color.black);
  101.  
  102.         namef = new JTextField();
  103.         emailf = new JTextField();
  104.         pwf = new JPasswordField();
  105.         repwf = new JPasswordField();
  106.  
  107.         namef.setBounds(150, 100, 300, 30);
  108.         namef.setBorder(border);
  109.         namef.setFont(font);
  110.         c.add(namef);
  111.  
  112.        
  113.         emailf.setBounds(150, 250, 300, 30);
  114.         emailf.setBorder(border);
  115.         emailf.setFont(font);
  116.         c.add(emailf);
  117.  
  118.        
  119.         pwf.setBounds(150, 300, 300, 30);
  120.         pwf.setBorder(border);
  121.         pwf.setFont(font);
  122.         c.add(pwf);
  123.  
  124.        
  125.         repwf.setBounds(600, 300, 300, 30);
  126.         repwf.setBorder(border);
  127.         repwf.setFont(font);
  128.         c.add(repwf);
  129.     }
  130.    
  131.     void addButton()
  132.     {
  133.         Font font2 = new Font("Arial", Font.BOLD, 18);
  134.  
  135.        
  136.        
  137.         clr = new JButton("Clear");
  138.         clr.setBounds(150, 350, 150, 50);
  139.         clr.setFont(font2);
  140.         clr.setOpaque(true);
  141.         clr.setBackground(Color.BLUE);
  142.         clr.setForeground(Color.WHITE);
  143.         c.add(clr);
  144.  
  145.         val = new JButton("Validate");
  146.         val.setBounds(400, 350, 150, 50);
  147.         val.setFont(font2);
  148.         val.setOpaque(true);
  149.         val.setBackground(Color.BLUE);
  150.         val.setForeground(Color.WHITE);
  151.         c.add(val);
  152.  
  153.         regs = new JButton("Register");
  154.         regs.setBounds(650, 350, 150, 50);
  155.         regs.setFont(font2);
  156.         regs.setOpaque(true);
  157.         regs.setBackground(Color.BLUE);
  158.         regs.setForeground(Color.WHITE);
  159.         regs.setEnabled(false);
  160.         c.add(regs);
  161.  
  162.         // actionlistener
  163.         clr.addActionListener(this);
  164.         val.addActionListener(this);
  165.         regs.addActionListener(this);
  166.     }
  167.    
  168.     void addComboBox()
  169.     {
  170.         day[0] = "day";
  171.         for( int i = 1; i <= 31; i++ ) {
  172.             String temp = ( Integer.toString(i) );
  173.             day[i] = temp;
  174.         }
  175.  
  176.         year[0] = "year";
  177.         for( int i = 1, j = 1900; i < 151; i++, j++ ) {
  178.             String temp = Integer.toString(j);
  179.             year[i] = temp;
  180.         }
  181.  
  182.         cb1 = new JComboBox<>( day );
  183.         cb2 = new JComboBox<>( month );
  184.         cb3 = new JComboBox<>( year );
  185.        
  186.         cb1.setBounds(150, 150, 100, 30);
  187.         cb1.setFont(font);
  188.         c.add(cb1);
  189.  
  190.        
  191.         cb2.setBounds(300, 150, 100, 30);
  192.         cb2.setFont(font);
  193.         c.add(cb2);
  194.  
  195.        
  196.         cb3.setBounds(450, 150, 100, 30);
  197.         cb3.setFont(font);
  198.         c.add(cb3);
  199.     }
  200.  
  201.     void addRadioButton()
  202.     {
  203.         bg = new ButtonGroup();
  204.  
  205.         male = new JRadioButton("Male");
  206.         female = new JRadioButton("Female");
  207.         other = new JRadioButton("Other");
  208.  
  209.         male.setBounds(150, 200, 100, 30);
  210.         male.setFont(font);
  211.         c.add(male);
  212.  
  213.        
  214.         female.setBounds(250, 200, 100, 30);
  215.         female.setFont(font);
  216.         c.add(female);
  217.  
  218.        
  219.         other.setBounds(350, 200, 100, 30);
  220.         other.setFont(font);
  221.         c.add(other);
  222.  
  223.         bg.add(male);
  224.         bg.add(female);
  225.         bg.add(other);
  226.     }
  227.  
  228.  
  229.  
  230.     public static void main(String[] args) throws Exception {
  231.        
  232.         problem2 frame = new problem2();
  233.         frame.setVisible(true);
  234.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  235.     }
  236.  
  237.     @Override
  238.     public void actionPerformed(ActionEvent e) {
  239.         if( e.getSource() == this.clr ) {
  240.             this.namef.setText(null);
  241.             this.emailf.setText(null);
  242.             this.pwf.setText(null);
  243.             this.repwf.setText(null);
  244.  
  245.             this.cb1.setModel(new DefaultComboBoxModel<String>(day));
  246.             this.cb2.setModel(new DefaultComboBoxModel<String>(month));
  247.             this.cb3.setModel(new DefaultComboBoxModel<String>(year));
  248.  
  249.             this.bg.clearSelection();
  250.  
  251.             stts.setText("Status: ");
  252.             regs.setEnabled(false);
  253.         }
  254.        
  255.         else if( e.getSource() == this.val ) {
  256.             stts.setText("Status: Validation Successfull!");
  257.             regs.setEnabled(true);
  258.             String temp = new String();
  259.             int i = 0;
  260.  
  261.             if( namef.getText().length() == 0 ) {
  262.                 i++;
  263.                 temp += "(" + Integer.toString(i) +") name ";
  264.             }
  265.             if( emailf.getText().length() == 0 ) {
  266.                 i++;
  267.                 temp += "(" + Integer.toString(i) +") email ";
  268.             }
  269.             if( pwf.getText().length() == 0 ) {
  270.                 i++;
  271.                 temp += "(" + Integer.toString(i) +") password ";
  272.             }
  273.             else if( !pwf.getText().toString().equals(repwf.getText().toString())  ) {
  274.                 System.out.println(pwf.getText().toString());
  275.                 System.out.println(repwf.getText().toString());
  276.                 i++;
  277.                 temp += "(" + Integer.toString(i) +") same password to both field ";
  278.             }
  279.  
  280.             String temp2 = new String();
  281.  
  282.             if( i != 0 ) {
  283.                 temp = "Please enter: " + temp;    
  284.                 stts.setText( temp );
  285.                 temp2 += " and select: ";
  286.                 regs.setEnabled(false);
  287.             }
  288.             else {
  289.                 temp2 += "Please select: ";
  290.                 regs.setEnabled(true);
  291.             }
  292.  
  293.             i = 0;
  294.  
  295.             if( cb1.getSelectedItem().toString() == "day" ) {
  296.                 i++;
  297.                 temp2 += "(" + Integer.toString(i) +") birth date ";
  298.             }
  299.             if( cb2.getSelectedItem().toString() == "month" ) {
  300.                 i++;
  301.                 temp2 += "(" + Integer.toString(i) +") birth month ";
  302.             }
  303.             if( cb3.getSelectedItem().toString() == "year" ) {
  304.                 i++;
  305.                 temp2 += "(" + Integer.toString(i) +") birth year ";
  306.             }
  307.             if( bg.getSelection() == null ) {
  308.                 i++;
  309.                 temp2 += "(" + Integer.toString(i) +") Gender ";
  310.             }
  311.  
  312.             if( i != 0 ) {
  313.                 stts.setText( "Status: " + temp + temp2 );
  314.                 regs.setEnabled(false);
  315.             }
  316.         }
  317.        
  318.         else if( e.getSource() == this.regs ) {
  319.             stts.setText("Status: Registration Successful!");
  320.         }
  321.     }
  322. }
  323.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement