hafidh

JAVA GUI - Coba action di checkbox dan Radiobutton

Feb 19th, 2012
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4.  
  5. class CobaAction extends JFrame{
  6.     // attribute
  7.     JRadioButton cb     = new JRadioButton("Makan");
  8.     JRadioButton cb2    = new JRadioButton("Ayam");
  9.     JRadioButton cb3    = new JRadioButton("goreng");
  10.     ButtonGroup group   = new ButtonGroup();
  11.     JCheckBox cekbox1   = new JCheckBox("S1");
  12.     JCheckBox cekbox2   = new JCheckBox("S2");
  13.     JCheckBox cekbox3   = new JCheckBox("S3");
  14.    
  15.     JLabel labelrb      = new JLabel("radio button yang dipilih ?");
  16.     JTextArea labelcb   = new JTextArea();
  17.     JScrollPane scrl    ;
  18.     // anda juga dapat meletakan tag HTML di label
  19.     JLabel ket1         = new JLabel("<html><u>Radio button</u></html>");
  20.     JLabel ket2         = new JLabel("<html><i>CheckBox</i></html>");
  21.    
  22.  
  23.     public CobaAction(){
  24.         //frame seting---------------------------------------------------------------------
  25.         //memanggil konstruktor kelas induk (JFrame)
  26.         super("Aksi chekbox dan radiobutton");
  27.         //seting besar frame LEBAR x TINGGI
  28.         this.setSize(300,350);
  29.         //seting agar bisa ditutup
  30.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  31.         //seting kemunculan frame di tengah window
  32.         this.setLocationRelativeTo(null);
  33.         //seting layout frame
  34.         this.getContentPane().setLayout(null);
  35.         //frame seting---------------------------------------------------------------------
  36.     }
  37.     public void konfig(){
  38.         this.add(ket1);
  39.         this.add(cb);
  40.         this.add(cb2);
  41.         this.add(cb3);
  42.         this.add(labelrb);
  43.         this.add(ket2);
  44.         this.add(cekbox1);
  45.         this.add(cekbox2);
  46.         this.add(cekbox3);
  47.         scrl = new JScrollPane(labelcb);
  48.         this.add(scrl);
  49.         // seting text area tidak bisa di input
  50.         labelcb.setEditable(false);
  51.         // jadikan radion button dalam 1 grup agar hanya 1 yang dapat dipilih
  52.         group.add(cb);
  53.         group.add(cb2);
  54.         group.add(cb3);
  55.         // kita set tata letak dan ukurannya
  56.         // setbounds (x,y,lebar/panjang,tinggi)
  57.         ket1.setBounds(10,5,150,30);
  58.         cb.setBounds(10,35,150,30);
  59.         cb2.setBounds(10,65,150,30);
  60.         cb3.setBounds(10,95,150,30);
  61.         labelrb.setBounds(10,125,250,30);
  62.         ket2.setBounds(10,155,150,30);
  63.         cekbox1.setBounds(10,185,150,30);
  64.         cekbox2.setBounds(10,215,150,30);
  65.         cekbox3.setBounds(10,245,150,30);
  66.         scrl.setBounds(160,185,120,90);
  67.     }
  68.     public void setAksi(){
  69.         // aksi cekbox
  70.         cb.addActionListener(new ActionListener( ) {
  71.             public void actionPerformed(ActionEvent e){
  72.                 // kode disini; cek keadaan check box
  73.                 labelrb.setText("Anda pilih makan");
  74.             }
  75.         });
  76.         cb2.addActionListener(new ActionListener( ) {
  77.             public void actionPerformed(ActionEvent e){
  78.                 // kode disini; cek keadaan check box
  79.                 labelrb.setText("Anda pilih ayam");
  80.             }
  81.         });
  82.         cb3.addActionListener(new ActionListener( ) {
  83.             public void actionPerformed(ActionEvent e){
  84.                 // kode disini; cek keadaan check box
  85.                 labelrb.setText("Anda pilih makan");
  86.             }
  87.         });
  88.         cekbox1.addActionListener(new ActionListener( ) {
  89.             public void actionPerformed(ActionEvent e){
  90.                 // kode disini; cek keadaan check box
  91.                 if(cekbox1.isSelected()){
  92.                     labelcb.append("-Anda sudah S1\n");
  93.                 }else{
  94.                     labelcb.append("-Anda belum S1\n");
  95.                 }
  96.                
  97.             }
  98.         });
  99.         cekbox2.addActionListener(new ActionListener( ) {
  100.             public void actionPerformed(ActionEvent e){
  101.                 // kode disini; cek keadaan check box
  102.                 if(cekbox2.isSelected()){
  103.                     labelcb.append("-Anda sudah S2\n");
  104.                 }else{
  105.                     labelcb.append("-Anda belum S2\n");
  106.                 }
  107.                
  108.             }
  109.         });
  110.         cekbox3.addActionListener(new ActionListener( ) {
  111.             public void actionPerformed(ActionEvent e){
  112.                 // kode disini; cek keadaan check box
  113.                 if(cekbox3.isSelected()){
  114.                     labelcb.append("-Anda sudah S3\n");
  115.                 }else{
  116.                     labelcb.append("-Anda belum S3\n");
  117.                 }
  118.                
  119.             }
  120.         });
  121.     }
  122.     public static void main(String[] args){
  123.         CobaAction act = new CobaAction();
  124.         act.konfig();
  125.         act.setAksi();
  126.         // metode show bawaan dari JFrame (kelas induk)
  127.         // untuk menampilkan Frame kelayar
  128.         act.show();
  129.     }
  130.    
  131. }
Add Comment
Please, Sign In to add comment