hafidh

JAVA GUI : Coba ComboBox

Nov 26th, 2011
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. // paket yang diperlukan
  2. import javax.swing.*;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5.  
  6. class TestComboBox extends JFrame{
  7.     // attribut
  8.     //membuat tombol
  9.     JButton tombol1 = new JButton("ambil nilai");
  10.     JButton tombol2 = new JButton("set nilai Combobox");
  11.     JLabel label = new JLabel("nilai Combobox");
  12.     // JComboBox(String nama)
  13.     String[] menu = {"Nasi","Ayam","Goreng"};
  14.     JComboBox cb = new JComboBox(menu);
  15.    
  16.     //konstruktor
  17.     public TestComboBox(){
  18.         //frame seting---------------------------------------------------------------------
  19.         //memanggil konstruktor kelas induk (JFrame)
  20.         super("Contoh Combobox");
  21.         //seting besar frame 400 x 400 px
  22.         this.setSize(200,200);
  23.         //seting agar bisa ditutup
  24.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  25.         //seting kemunculan frame di tengah window
  26.         this.setLocationRelativeTo(null);
  27.         //seting layout frame
  28.         this.getContentPane().setLayout(null);
  29.         //frame seting---------------------------------------------------------------------
  30.         //menambahkan komponen ke frame dan seting letak
  31.         this.add(tombol1);
  32.         this.add(tombol2);
  33.         this.add(label);
  34.         this.add(cb);
  35.         // setbounds(x,y,lebar,tinggi)
  36.         tombol1.setBounds(10,10,150,25);
  37.         tombol2.setBounds(10,40,150,25);
  38.         label.setBounds(10,70,150,25);
  39.         cb.setBounds(10,110,150,25);
  40.         //seting aksi tombol
  41.         tombol1.addActionListener(new ActionListener( ) {
  42.             public void actionPerformed(ActionEvent e){
  43.                 // kode disini; cek keadaan check box
  44.                 int i = cb.getSelectedIndex();
  45.                 String str = "";
  46.                 switch(i){
  47.                     case 0: str = "anda mau makan "+menu[i]; break;
  48.                     case 1: str = "anda makan "+menu[i]+" bakar"; break;
  49.                     case 2: str = menu[i]+" bakwan"; break;
  50.                 }
  51.                 label.setText(str);
  52.             }
  53.         });
  54.         tombol2.addActionListener(new ActionListener( ) {
  55.             public void actionPerformed(ActionEvent e){
  56.                 // kode disini; cek keadaan check box ganti keadaan chek box
  57.                 cb.setSelectedIndex(2);
  58.                 label.setText("anda memilih index-2");
  59.             }
  60.         });
  61.         // menampilkan frame
  62.         show();
  63.        
  64.     }
  65.    
  66.     public static void main(String[] args){
  67.         new TestComboBox();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment