Source of Oracle_save.java


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