home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap09 / Applet7.java < prev    next >
Encoding:
Java Source  |  1996-02-05  |  1.2 KB  |  51 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class Applet7 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.         switch(choice)
  25.         {
  26.             case 1:
  27.                 g.setColor(Color.red);
  28.                 break;
  29.             case 2:
  30.                 g.setColor(Color.blue);
  31.                 break;
  32.             case 3:
  33.                 g.setColor(Color.green);
  34.                 break;
  35.             default:
  36.                 g.setColor(Color.black);
  37.         }
  38.  
  39.         if ((choice >= 1) && (choice <= 3))
  40.             g.drawString("This is the color you chose.", 60, 140);
  41.         else
  42.             g.drawString("Invalid menu selection.", 60, 140);
  43.     }
  44.  
  45.     public boolean action(Event event, Object arg)
  46.     {
  47.         repaint();
  48.         return true;
  49.     }
  50. }
  51.