Program Java with Jcombo Box



Process Input Data Programme




Script :

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>Name:</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>Date of Birth:</font></html>";
                   labelLahir = new JLabel (s2);
                   labelLahir.setLocation(10, 55);
                   labelLahir.setSize(labelLahir.getPreferredSize());
                  
                   labelTanggal = new JLabel(" Day to   " );
                   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 ("Month");
                   labelBulan.setLocation(95, 73);
                   labelBulan.setSize(labelBulan.getPreferredSize());
                  
                   comboBulan = new JComboBox();
                   comboBulan.setLocation(95, 93);
                   comboBulan.setSize(labelLahir.getPreferredSize());
                   String[] bulan = {"January","February","March","April","May","June","July","August","September","October","November","December"};
                   for (int i=0; i<bulan.length; i++) {
                             comboBulan.addItem(bulan[i]);
                   }
                   labelTahun = new JLabel ("Year");
                   labelTahun.setLocation(190, 75);
                   labelTahun.setSize(labelTahun.getPreferredSize());
                  
                   comboTahun = new JComboBox();
                   comboTahun.setLocation(190, 93);
                   comboTahun.setSize(labelLahir.getPreferredSize());
                   for (int i=1960; i<=2013; i++) {
                             comboTahun.addItem(new String().valueOf(i));
                   }
                  
                   btnProses = new JButton("Data Process");
                   btnProses.setLocation(305, 25);
                   btnProses.setSize(btnProses.getPreferredSize());
                   btnProses.addActionListener(this);
                   btnProses.setMnemonic('P');

                   tfInfo = new JTextField(50);
                   tfInfo.setLocation(20, 290);
                   tfInfo.setSize(400, 20);
                    tfInfo.setEditable(false);
          }
          public void createAndShowGUI() {
                   JFrame.setDefaultLookAndFeelDecorated(true);
                   JFrame frame = new JFrame ("Data Input with 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();
                             }
                   });
          }
 
}



 Berikut cara penulisan script pada JCreator :







Ini adalah output dari script diatas :






 

0 Response to "Program Java with Jcombo Box"

Posting Komentar