Home | Looking for something? Sign In | New here? Sign Up | Log out

Kamis, 08 Juli 2010

Program Java Sederhana (ComboBox)

Kamis, 08 Juli 2010

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 = "Nama:";

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 =

"Tanggal Lahir:";

labelLahir = new JLabel(s2);

labelLahir.setLocation(10,55);

labelLahir.setSize(labelLahir.getPreferredSize());

labelTanggal = new JLabel("Hari Ke :");

labelTanggal.setLocation(10,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","Pebruari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","Nopember","Desember"};

for (int i=0;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(100,250);

tfInfo.setSize(250,25);

tfInfo.setEditable(false);

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Contoh 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, 450, 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();

}

});

}

}

HASILNYA :



0 komentar:

:10 :11 :12 :13
:14 :15 :16 :17
:18 :19 :20 :21
:22 :23 :24 :25
:26 :27 :28 :29
:30 :31 :32 :33
:34 :35 :36 :37
:38 :39 :40 :41
:42 :43 :44 :45

Posting Komentar