Advertisement
akevintg

Registration Form

Dec 6th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7.  
  8.  
  9. public class Regis extends JFrame implements ActionListener, KeyListener{
  10.  
  11.     Regis(){
  12.         setFocusable(true);
  13.         template();
  14.         frame();
  15.         pack();
  16.         setLocationRelativeTo(null);
  17.     }
  18.    
  19.     void frame(){
  20.         setVisible(true);
  21.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  22.         setTitle("Form Registrasi");    
  23.     }
  24.     //layout
  25.     JPanel form=new JPanel(new GridLayout(9,1));
  26.     JPanel hobi = new JPanel(new GridLayout(3,1));
  27.     JPanel cit = new JPanel(new GridLayout(1,2));
  28.     JPanel gender = new JPanel(new GridLayout(1,2));
  29.     JPanel tombol = new JPanel(new GridLayout(1,4));
  30.     JPanel b1 = new JPanel(new GridLayout(1,2));
  31.     JPanel b2 = new JPanel(new GridLayout(1,2));
  32.     JPanel b3 = new JPanel(new GridLayout(1,2));
  33.     JPanel b4 = new JPanel(new GridLayout(1,2));
  34.     JPanel b5 = new JPanel(new GridLayout(1,2));
  35.     JPanel b6 = new JPanel(new GridLayout(1,2));
  36.     JPanel b7 = new JPanel(new GridLayout(1,2));
  37.     //font
  38.     Font f=new Font("SansSerif",Font.BOLD, 50);
  39.     //component
  40.     JLabel id=new JLabel();
  41.     JPasswordField pass1=new JPasswordField();
  42.     JPasswordField pass2=new JPasswordField();
  43.     JRadioButton male=new JRadioButton("Male");
  44.     JRadioButton female=new JRadioButton("Female");
  45.     ButtonGroup gen=new ButtonGroup();
  46.     JCheckBox bdm=new JCheckBox("Badminton");
  47.     JCheckBox cdg=new JCheckBox("Coding");
  48.     JCheckBox drw=new JCheckBox("Drawing");
  49.     String[] citizen = { "","Indonesia","Other"};
  50.     JComboBox ctz=new JComboBox(citizen);
  51.     JLabel gambar=new JLabel();
  52.     String[] item = { "item 1","item 2","item 3","item 4","item 5","item 6","item 7"};
  53.     JList itm=new JList(item);
  54.     JScrollPane items=new JScrollPane(itm);
  55.     JButton submit = new JButton("Submit");
  56.     JButton reset = new JButton("Reset");
  57.    
  58.     void template(){
  59.         //baris 1 judul
  60.         JLabel header=new JLabel("REGISTRATION");
  61.         header.setFont(f);
  62.         form.add(header);
  63.         //baris 2 user id : .....
  64.         b1.add(new JLabel("User ID"));
  65.         b1.add(id);
  66.         form.add(b1);
  67.         //baris 3 password:.....
  68.         b2.add(new JLabel("Password"));
  69.         b2.add(pass1);
  70.         form.add(b2);
  71.         //baris 3 Confirmation password:.....
  72.         b3.add(new JLabel("Confirmation Password"));
  73.         b3.add(pass2);pass2.addKeyListener(this);
  74.         form.add(b3);
  75.         //baris 4 gender
  76.         b4.add(new JLabel("Gender"));
  77.         gen.add(male);male.addActionListener(this);
  78.         gen.add(female);female.addActionListener(this);
  79.         gender.add(male);
  80.         gender.add(female);
  81.         b4.add(gender);
  82.         form.add(b4);
  83.         //baris 5 Hobby
  84.         b5.add(new JLabel("Hobby"));
  85.         hobi.add(bdm);
  86.         hobi.add(cdg);
  87.         hobi.add(drw);
  88.         b5.add(hobi);
  89.         form.add(b5);
  90.         //baris 6 citizenship
  91.         b6.add(new JLabel("Citizendhip"));
  92.         cit.add(ctz);ctz.addActionListener(this);
  93.         cit.add(gambar);
  94.         b6.add(cit);
  95.         form.add(b6);
  96.         //baris 7 citizenship
  97.         b7.add(new JLabel("Item"));
  98.         b7.add(items);
  99.         form.add(b7);
  100.         //baris 8 tombol
  101.         tombol.add(new JLabel(" "));
  102.         tombol.add(submit);submit.addActionListener(this);
  103.         tombol.add(reset);reset.addActionListener(this);
  104.         tombol.add(new JLabel(" "));
  105.         form.add(tombol);
  106.         //kasih margin masukin ke frame
  107.         form.setBorder(BorderFactory.createEmptyBorder(10,25,10,25));
  108.         add(form);
  109.     }
  110.    
  111.     public static void main(String[] args) {
  112.         new Regis();
  113.     }
  114.  
  115.     @Override
  116.     public void actionPerformed(ActionEvent e) {
  117.         if(e.getActionCommand().equals("Male"))//cek kalo male
  118.             id.setText("M001");
  119.         else if(e.getActionCommand().equals("Female"))//cek kalo female
  120.             id.setText("F001");
  121.         else if (ctz.getSelectedIndex()==1&&ctz.getActionCommand().equals("comboBoxChanged"))
  122.             //syarat1 :kalo indexnya 1, syarat2: cek hanya saatlagi diubah
  123.             gambar.setIcon(new ImageIcon("Indonesia.jpg"));
  124.         else if (ctz.getActionCommand().equals("comboBoxChanged"))
  125.             //kalo lagi diubah tapi ga indonesia
  126.             gambar.setIcon(new ImageIcon(""));
  127.         if(e.getActionCommand().equals("Reset"))
  128.             reset();
  129.         else if(e.getActionCommand().equals("Submit")){
  130.             if (check()) {
  131.                 int jawaban= JOptionPane.showConfirmDialog(null, "Data Benar, Submit?","OK",JOptionPane.YES_NO_OPTION);
  132.                 if(jawaban==JOptionPane.YES_OPTION)
  133.                     System.exit(0);
  134.             }
  135.             else
  136.                 JOptionPane.showMessageDialog(null, "Data harus dipilih");
  137.         }
  138.         System.out.println(e.getActionCommand());
  139.     }
  140.  
  141.     boolean check(){
  142.         if(id.getText().equals("")||
  143.             pass1.getText().equals("")||
  144.             !pass1.getText().equals(pass2.getText())||
  145.             (!bdm.isSelected()&&!cdg.isSelected()&&!drw.isSelected())||
  146.             ctz.getSelectedIndex()==0||itm.isSelectionEmpty())
  147.         return false;
  148.         else
  149.             return true;
  150.     }
  151.     void reset(){
  152.         id.setText("");
  153.         pass1.setText("");
  154.         pass2.setText("");
  155.         gen.clearSelection();
  156.         bdm.setSelected(false);
  157.         cdg.setSelected(false);
  158.         drw.setSelected(false);
  159.         ctz.setSelectedIndex(0);
  160.         itm.clearSelection();
  161.     }
  162.     @Override
  163.     public void keyTyped(KeyEvent e) {
  164.     }
  165.  
  166.     @Override
  167.     public void keyPressed(KeyEvent e) {
  168.     }
  169.  
  170.     @Override
  171.     public void keyReleased(KeyEvent e) {
  172.         if(pass1.getText().equals(pass2.getText())&&!(e.getKeyCode()=='\n'))
  173.             JOptionPane.showMessageDialog(null, "Sama");
  174.     }
  175.    
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement