Source of Oracle.java


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