//import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Horoscope1
{
// the following fields need to be accessed from inside the
// event handler methods, so need to be static
// Horoscope messages
private static String m1 = "message 1";
private static String m2 = "message 2";
private static String m3 = "message 3";
private static String m4 = "message 4";
private static String m5 = "message 5";
private static String m6 = "message 6";
private static String m7 = "message 7";
private static String m8 = "message 8";
private static String m9 = "message 9";
private static String m10 = "message 10";
private static String m11 = "message 11";
private static String m12 = "message 12";
private static JTextField userInputField; // Holds horoscope sign entered by user
private static JLabel response; // Holds horoscope and lucky number
private static int luckyNumber; // Holds the random lucky number
// Define Event Listener for both buttons.
// Perform different actions based on button clicked.
private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event)
{
String whichButton; // Holds the button's name
// Get the button's name.
// This is how we tell which button has been clicked.
whichButton = event.getActionCommand ( );
if ( whichButton.equals ( "Give horoscope" ) ) {
// Get horoscope sign the user typed in data field
String horoscopeSign = userInputField.getText();
// Generate a lucky number from 1 to 100
luckyNumber = (int) (Math.random ( ) * 100.0 ) + 1;
if (horoscopeSign.equals(""))
response.setText("Enter a sign first");
else if(horoscopeSign.equals("aries"))
response.setText(m1);
else if(horoscopeSign.equals("taurus"))
response.setText(m2);
else if(horoscopeSign.equals("gemini"))
response.setText(m3);
else if(horoscopeSign.equals("cancer"))
response.setText(m4);
else if(horoscopeSign.equals("leo"))
response.setText(m5);
else if(horoscopeSign.equals("virgo"))
response.setText(m6);
else if(horoscopeSign.equals("libra"))
response.setText(m7);
else if(horoscopeSign.equals("scorpio"))
response.setText(m8);
else if(horoscopeSign.equals("sagittarius"))
response.setText(m9);
else if(horoscopeSign.equals("capricorn"))
response.setText(m10);
else if(horoscopeSign.equals("aquarius"))
response.setText(m11);
else if(horoscopeSign.equals("pisces"))
response.setText(m12);
else
response.setText("Not one of the 12 signs. Try again");
} // end of if whichButton.equals "Give horoscope"
// Else assume the button clicked is Reset.
// Better code would be to verify by comparing to "Reset".
else {
// Reset the user input data field
userInputField.setText("");
// Reset the response message field
response.setText("");
}
} // end of actionPerformed () method
} // end of ButtonHandler class
public static void main(String[] args)
{
// Instantiate new JFrame object.
// Title for frame is optional.
JFrame ourFrame=new JFrame("Your Horoscope");
ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ourFrame.setSize(800,400);
// Get the content pane and set layout
Container ourPane=ourFrame.getContentPane();
ourPane.setLayout(new GridLayout(3,3));
// Instantiate user instruction text.
JLabel instruction=new JLabel(
"FREE Daily Horoscope
" +
"Enter one of the following signs on the right and
" +
"press the \"Give horoscope\" button.
" +
"aries, taurus, gemini, cancer, leo, virgo, libra,
" +
"scorpio, sagittarius, capricorn, aquarius, pisces. ");
// Instantiate field for user to enter horoscope sign
userInputField = new JTextField("", 12);
// Create button for user to click after entering the sign
JButton giveHoroscopeButton=new JButton("Give horoscope");
// Create button to reset fields
JButton resetButton=new JButton("Reset");
// Instantiate new event listener
ButtonHandler ourEventListener=new ButtonHandler();
// Register our event listener with the both buttons
giveHoroscopeButton.addActionListener(ourEventListener);
resetButton.addActionListener(ourEventListener);
// Instantiate text response field and initialize to blank.
response=new JLabel("", JLabel.CENTER);
// Place components in frame and make it visible
ourPane.add(instruction);
ourPane.add(userInputField);
ourPane.add(giveHoroscopeButton);
ourPane.add(response);
ourPane.add(resetButton);
ourFrame.setVisible(true);
}
}