Source of Oracle.java


  1: //Oracle.java
  2: 
  3: import javax.swing.*;
  4: import java.awt.*;
  5: import java.awt.event.*;
  6: 
  7: public class Oracle extends JApplet implements ActionListener
  8: {
  9:     public static int LINES = 5;
 10:     public static int CHAR_PER_LINE = 40;
 11: 
 12:     private JTextArea theText;
 13:     private String question;
 14:     private String answer;
 15:     private String advice;
 16: 
 17:     public void init()
 18:     {
 19:         Container contentPane = getContentPane( );
 20:         contentPane.setLayout(new FlowLayout( ));
 21: 
 22:         JLabel instructions1 =
 23:             new JLabel("I will answer any question, " +
 24:                       "but may need some advice from you.");
 25:         JLabel instructions2 =
 26:             new JLabel("Questions and advice must be entered " +
 27:                       "in the empty textbox you see below.");
 28:         contentPane.add(instructions1);
 29:         contentPane.add(instructions2);
 30: 
 31:         JButton getAnswerButton = new JButton("Get Answer");
 32:         getAnswerButton.addActionListener(this);
 33:         contentPane.add(getAnswerButton);
 34: 
 35:         JButton sendAdviceButton = new JButton("Send Advice");
 36:         sendAdviceButton.addActionListener(this);
 37:         contentPane.add(sendAdviceButton);
 38: 
 39:         JButton resetButton = new JButton("Reset");
 40:         resetButton.addActionListener(this);
 41:         contentPane.add(resetButton);
 42: 
 43:         theText = new JTextArea(LINES, CHAR_PER_LINE);
 44:         /*
 45:         theText.setText("Questions and advice go here.");
 46:         contentPane.add(theText);
 47:         */
 48:         theText.setText("");
 49:         contentPane.add(theText);
 50: 
 51:         answer = 
 52:             "The answer is: Look around."; //for first answer
 53:     }
 54: 
 55:     public void actionPerformed(ActionEvent e)
 56:     {
 57:         String actionCommand = e.getActionCommand( );
 58:         if (actionCommand.equals("Get Answer"))
 59:         {
 60:             question = theText.getText();
 61:             theText.setText("That is a difficult question.\n" +
 62:                      "I need some advice.\n" +
 63:                      "Give me some advice and click button:\n");
 64:         }
 65:         else if(actionCommand.equals("Send Advice"))
 66:         {
 67:             advice = 
 68:                 theText.getText().substring(theText.getText().indexOf(":")+2);
 69:             theText.setText("That advice helped.\n" +
 70:                          "You asked the question: " + question
 71:                          + "\n" + answer +
 72:                          "\n\nNow click the Reset button and " +
 73:                          "leave the program on for others.");
 74:             answer = "The answer is: " + advice;
 75:         }
 76:         else if(actionCommand.equals("Reset"))
 77:         {
 78:             theText.setText("");
 79:         }
 80:         else
 81:             theText.setText("Error");
 82:      }
 83: }
 84: