/* * File: ComboBoxTest.java * * Create a Combo box with single selection , using ActionEvents * for (singly) selected items. * * Copyright: Northeast Parallel Architectures Center * */ import javax.swing.*; import java.awt.event.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.lang.*; import java.awt.*; import java.awt.event.*; public class ComboBoxTest extends JApplet implements ActionListener { private JComboBox Month,bDate; private String[] sMonth ={"January","February","March","April","May","June","July","August","September","October","November","December"}; private String[] sDate = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"}; private String[] Zodiac ={"I don't know","Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius","Capricorn","Aquarius","Pisces"}; String resulttext = "Ur Zodiac sign is "; JLabel result = new JLabel(resulttext); private Font f = new Font ("Dialog", Font.BOLD, 24); private JLabel label1 = new JLabel("Select your Month of Birth "); private JLabel label2 = new JLabel("Select your Date of Birth "); String item1="" ; //for storing month int item2=0;//for storing day of the month public void init() { getContentPane().setLayout(new FlowLayout()); Month = new JComboBox(sMonth); Month.setEditable(true); bDate = new JComboBox(sDate); bDate.setEditable(true); result.setFont(f); getContentPane().add(label1); getContentPane().add(Month); getContentPane().add(label2); getContentPane().add(bDate); getContentPane().add(result); Month.addActionListener(this); bDate.addActionListener(this); } public void actionPerformed(ActionEvent event) { if( event.getSource() == Month) item1=(String) Month.getSelectedItem(); if( event.getSource() == bDate) item2 =Integer.parseInt((String) bDate.getSelectedItem()); if (item1!= "" && item2 !=0) calculate_zodiac(item1,item2); } //Calculates the Zodiac based on the month and date public void calculate_zodiac(String bmonth,int bday) { int choice = 0; if (bmonth.compareToIgnoreCase("January") == 0) { if (bday < 23) choice = 10; else choice = 11; } if (bmonth.compareToIgnoreCase("February") == 0) { if (bday < 23) choice = 11; else choice = 12; } if (bmonth.compareToIgnoreCase("March") == 0) { if (bday < 23) choice = 12; else choice = 1; } if (bmonth.compareToIgnoreCase("April") == 0) { if (bday < 23) choice = 1; else choice = 2; } if (bmonth.compareToIgnoreCase("May") == 0) { if (bday < 23) choice = 2; else choice = 3; } if (bmonth.compareToIgnoreCase("June") == 0) { if (bday < 23) choice = 3; else choice = 4; } if (bmonth.compareToIgnoreCase("July") == 0) { if (bday < 23) choice = 4; else choice = 5; } if (bmonth.compareToIgnoreCase("August") == 0) { if (bday < 23) choice = 5; else choice = 6; } if (bmonth.compareToIgnoreCase("September") == 0) { if (bday < 23) choice = 6; else choice = 7; } if (bmonth.compareToIgnoreCase("October") == 0) { if (bday < 23) choice = 7; else choice = 8; } if (bmonth.compareToIgnoreCase("November") == 0) { if (bday < 23) choice = 8; else choice = 9; } if (bmonth.compareToIgnoreCase("December") == 0) { if (bday < 23) choice = 9; else choice = 10; } result.setText(resulttext + Zodiac[choice]); } }