Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // paket yang diperlukan
- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- class TestComboBox extends JFrame{
- // attribut
- //membuat tombol
- JButton tombol1 = new JButton("ambil nilai");
- JButton tombol2 = new JButton("set nilai Combobox");
- JLabel label = new JLabel("nilai Combobox");
- // JComboBox(String nama)
- String[] menu = {"Nasi","Ayam","Goreng"};
- JComboBox cb = new JComboBox(menu);
- //konstruktor
- public TestComboBox(){
- //frame seting---------------------------------------------------------------------
- //memanggil konstruktor kelas induk (JFrame)
- super("Contoh Combobox");
- //seting besar frame 400 x 400 px
- this.setSize(200,200);
- //seting agar bisa ditutup
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //seting kemunculan frame di tengah window
- this.setLocationRelativeTo(null);
- //seting layout frame
- this.getContentPane().setLayout(null);
- //frame seting---------------------------------------------------------------------
- //menambahkan komponen ke frame dan seting letak
- this.add(tombol1);
- this.add(tombol2);
- this.add(label);
- this.add(cb);
- // setbounds(x,y,lebar,tinggi)
- tombol1.setBounds(10,10,150,25);
- tombol2.setBounds(10,40,150,25);
- label.setBounds(10,70,150,25);
- cb.setBounds(10,110,150,25);
- //seting aksi tombol
- tombol1.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- int i = cb.getSelectedIndex();
- String str = "";
- switch(i){
- case 0: str = "anda mau makan "+menu[i]; break;
- case 1: str = "anda makan "+menu[i]+" bakar"; break;
- case 2: str = menu[i]+" bakwan"; break;
- }
- label.setText(str);
- }
- });
- tombol2.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box ganti keadaan chek box
- cb.setSelectedIndex(2);
- label.setText("anda memilih index-2");
- }
- });
- // menampilkan frame
- show();
- }
- public static void main(String[] args){
- new TestComboBox();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment