Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class Regis extends JFrame implements ActionListener, KeyListener{
- Regis(){
- setFocusable(true);
- template();
- frame();
- pack();
- setLocationRelativeTo(null);
- }
- void frame(){
- setVisible(true);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setTitle("Form Registrasi");
- }
- //layout
- JPanel form=new JPanel(new GridLayout(9,1));
- JPanel hobi = new JPanel(new GridLayout(3,1));
- JPanel cit = new JPanel(new GridLayout(1,2));
- JPanel gender = new JPanel(new GridLayout(1,2));
- JPanel tombol = new JPanel(new GridLayout(1,4));
- JPanel b1 = new JPanel(new GridLayout(1,2));
- JPanel b2 = new JPanel(new GridLayout(1,2));
- JPanel b3 = new JPanel(new GridLayout(1,2));
- JPanel b4 = new JPanel(new GridLayout(1,2));
- JPanel b5 = new JPanel(new GridLayout(1,2));
- JPanel b6 = new JPanel(new GridLayout(1,2));
- JPanel b7 = new JPanel(new GridLayout(1,2));
- //font
- Font f=new Font("SansSerif",Font.BOLD, 50);
- //component
- JLabel id=new JLabel();
- JPasswordField pass1=new JPasswordField();
- JPasswordField pass2=new JPasswordField();
- JRadioButton male=new JRadioButton("Male");
- JRadioButton female=new JRadioButton("Female");
- ButtonGroup gen=new ButtonGroup();
- JCheckBox bdm=new JCheckBox("Badminton");
- JCheckBox cdg=new JCheckBox("Coding");
- JCheckBox drw=new JCheckBox("Drawing");
- String[] citizen = { "","Indonesia","Other"};
- JComboBox ctz=new JComboBox(citizen);
- JLabel gambar=new JLabel();
- String[] item = { "item 1","item 2","item 3","item 4","item 5","item 6","item 7"};
- JList itm=new JList(item);
- JScrollPane items=new JScrollPane(itm);
- JButton submit = new JButton("Submit");
- JButton reset = new JButton("Reset");
- void template(){
- //baris 1 judul
- JLabel header=new JLabel("REGISTRATION");
- header.setFont(f);
- form.add(header);
- //baris 2 user id : .....
- b1.add(new JLabel("User ID"));
- b1.add(id);
- form.add(b1);
- //baris 3 password:.....
- b2.add(new JLabel("Password"));
- b2.add(pass1);
- form.add(b2);
- //baris 3 Confirmation password:.....
- b3.add(new JLabel("Confirmation Password"));
- b3.add(pass2);pass2.addKeyListener(this);
- form.add(b3);
- //baris 4 gender
- b4.add(new JLabel("Gender"));
- gen.add(male);male.addActionListener(this);
- gen.add(female);female.addActionListener(this);
- gender.add(male);
- gender.add(female);
- b4.add(gender);
- form.add(b4);
- //baris 5 Hobby
- b5.add(new JLabel("Hobby"));
- hobi.add(bdm);
- hobi.add(cdg);
- hobi.add(drw);
- b5.add(hobi);
- form.add(b5);
- //baris 6 citizenship
- b6.add(new JLabel("Citizendhip"));
- cit.add(ctz);ctz.addActionListener(this);
- cit.add(gambar);
- b6.add(cit);
- form.add(b6);
- //baris 7 citizenship
- b7.add(new JLabel("Item"));
- b7.add(items);
- form.add(b7);
- //baris 8 tombol
- tombol.add(new JLabel(" "));
- tombol.add(submit);submit.addActionListener(this);
- tombol.add(reset);reset.addActionListener(this);
- tombol.add(new JLabel(" "));
- form.add(tombol);
- //kasih margin masukin ke frame
- form.setBorder(BorderFactory.createEmptyBorder(10,25,10,25));
- add(form);
- }
- public static void main(String[] args) {
- new Regis();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand().equals("Male"))//cek kalo male
- id.setText("M001");
- else if(e.getActionCommand().equals("Female"))//cek kalo female
- id.setText("F001");
- else if (ctz.getSelectedIndex()==1&&ctz.getActionCommand().equals("comboBoxChanged"))
- //syarat1 :kalo indexnya 1, syarat2: cek hanya saatlagi diubah
- gambar.setIcon(new ImageIcon("Indonesia.jpg"));
- else if (ctz.getActionCommand().equals("comboBoxChanged"))
- //kalo lagi diubah tapi ga indonesia
- gambar.setIcon(new ImageIcon(""));
- if(e.getActionCommand().equals("Reset"))
- reset();
- else if(e.getActionCommand().equals("Submit")){
- if (check()) {
- int jawaban= JOptionPane.showConfirmDialog(null, "Data Benar, Submit?","OK",JOptionPane.YES_NO_OPTION);
- if(jawaban==JOptionPane.YES_OPTION)
- System.exit(0);
- }
- else
- JOptionPane.showMessageDialog(null, "Data harus dipilih");
- }
- System.out.println(e.getActionCommand());
- }
- boolean check(){
- if(id.getText().equals("")||
- pass1.getText().equals("")||
- !pass1.getText().equals(pass2.getText())||
- (!bdm.isSelected()&&!cdg.isSelected()&&!drw.isSelected())||
- ctz.getSelectedIndex()==0||itm.isSelectionEmpty())
- return false;
- else
- return true;
- }
- void reset(){
- id.setText("");
- pass1.setText("");
- pass2.setText("");
- gen.clearSelection();
- bdm.setSelected(false);
- cdg.setSelected(false);
- drw.setSelected(false);
- ctz.setSelectedIndex(0);
- itm.clearSelection();
- }
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- if(pass1.getText().equals(pass2.getText())&&!(e.getKeyCode()=='\n'))
- JOptionPane.showMessageDialog(null, "Sama");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement