home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap09 / Applet6.java < prev    next >
Encoding:
Java Source  |  1996-04-24  |  1.1 KB  |  45 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class Applet6 extends Applet
  5. {
  6.     TextField textField1;
  7.  
  8.     public void init()
  9.     {
  10.         textField1 = new TextField(5);
  11.         add(textField1);
  12.         textField1.setText("1");
  13.     }
  14.  
  15.     public void paint(Graphics g)
  16.     {
  17.         g.drawString("Type a menu choice in the above box.", 20, 50);
  18.         g.drawString("1. Red", 40, 75);
  19.         g.drawString("2. Blue", 40, 95);
  20.         g.drawString("3. Green", 40, 115);
  21.         String s = textField1.getText();
  22.         int choice = Integer.parseInt(s);
  23.  
  24.         if (choice == 1)
  25.             g.setColor(Color.red);
  26.         else if (choice == 2)
  27.             g.setColor(Color.blue);
  28.         else if (choice == 3)
  29.             g.setColor(Color.green);
  30.         else
  31.             g.setColor(Color.black);
  32.  
  33.         if ((choice >= 1) && (choice <= 3))
  34.             g.drawString("This is the color you chose.", 60, 140);
  35.         else
  36.             g.drawString("Invalid menu selection.", 60, 140);
  37.     }
  38.  
  39.     public boolean action(Event event, Object arg)
  40.     {
  41.         repaint();
  42.         return true;
  43.     }
  44. }
  45.