Selasa, 14 Agustus 2012

Demo Combo Box


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DemoComboBox implements ActionListener {   
  public JLabel labelNama, labelLahir,
                labelTanggal, labelBulan, labelTahun;
  public JTextField tfNama, tfInfo;
  public JComboBox comboTanggal, comboBulan, comboTahun;
  public JButton btnProses;
       
  public DemoComboBox() {           
    String s1 = "<html><font color=red>Nama:</font></html>";
    labelNama = new JLabel(s1);
    labelNama.setLocation(10, 10);
    labelNama.setSize(labelNama.getPreferredSize());   
       
    tfNama = new JTextField(25);
    tfNama.setLocation(10, 30);
    tfNama.setSize(tfNama.getPreferredSize());   
       
    String s2 =
      "<html><font color=red>Tanggal Lahir:</font></html>";
    labelLahir = new JLabel(s2);
    labelLahir.setLocation(10, 55);
    labelLahir.setSize(labelLahir.getPreferredSize());   
       
    labelTanggal = new JLabel("Hari ke-");
    labelTanggal.setLocation(35, 75);
    labelTanggal.setSize(labelTanggal.getPreferredSize());
       
    comboTanggal = new JComboBox();
    comboTanggal.setLocation(35, 93);
    comboTanggal.setSize(labelTanggal.getPreferredSize());
    for (int i=0; i<31; i++) {
      comboTanggal.addItem(new String().valueOf(i+1));
    }
       
    labelBulan = new JLabel("Bulan");
    labelBulan.setLocation(95, 75);
    labelBulan.setSize(labelBulan.getPreferredSize());
       
    comboBulan = new JComboBox();
    comboBulan.setLocation(95, 93);
    comboBulan.setSize(labelLahir.getPreferredSize());
    String[] bulan = {"Januari","Februari","Maret","April",
               "Mei","Juni","Juli","Agustus",
               "September","Oktober","November","Desember"};
    for (int i=0; i<bulan.length; i++) {
      comboBulan.addItem(bulan[i]);
    }
       
    labelTahun = new JLabel("Tahun");
    labelTahun.setLocation(190, 75);
    labelTahun.setSize(labelTahun.getPreferredSize());
       
    comboTahun = new JComboBox();
    comboTahun.setLocation(190, 93);
    comboTahun.setSize(labelLahir.getPreferredSize());
    for (int i=1960; i<=2007; i++) {
      comboTahun.addItem(new String().valueOf(i));
    }   
                   
    btnProses = new JButton("Proses Data");
    btnProses.setLocation(305, 25);
    btnProses.setSize(btnProses.getPreferredSize()); 
    btnProses.addActionListener(this);
    btnProses.setMnemonic('P');   
       
    tfInfo = new JTextField(50);
    tfInfo.setLocation(10, 290);
    tfInfo.setSize(400, 20);
    tfInfo.setEditable(false);
  }   
   
  public void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Demo JComboBox");
    frame.setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(labelNama);
    frame.getContentPane().add(tfNama);
    frame.getContentPane().add(labelLahir);
    frame.getContentPane().add(labelTanggal);
    frame.getContentPane().add(comboTanggal);
    frame.getContentPane().add(labelBulan);
    frame.getContentPane().add(comboBulan);
    frame.getContentPane().add(labelTahun);
    frame.getContentPane().add(comboTahun);
    frame.getContentPane().add(btnProses);
    frame.getContentPane().add(tfInfo);
       
    frame.setBounds(0, 0, 430, 350);
    frame.setLocationRelativeTo(null);

    frame.setVisible(true);
  }
   
  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == btnProses) {
      String s = tfNama.getText() + ", lahir pada " +
                 comboTanggal.getSelectedItem() + " " +
              comboBulan.getSelectedItem() + " " +
              comboTahun.getSelectedItem();           
     tfInfo.setText(s);           
    }   
  }
   
  public static void main(String[] args) {       
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        DemoComboBox app = new DemoComboBox();
        app.createAndShowGUI();
      }
    });       
  }   
}

Tidak ada komentar:

Posting Komentar