home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap28 / ConnectApplet.java < prev    next >
Text File  |  1996-03-15  |  1KB  |  61 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.net.*;
  4.  
  5. public class ConnectApplet extends Applet
  6. {
  7.     TextField textField;
  8.     boolean badURL;
  9.  
  10.     public void init()
  11.     {
  12.         textField = new TextField("", 40);
  13.         Button button = new Button("Connect");
  14.  
  15.         add(textField);
  16.         add(button);
  17.  
  18.         badURL = false;
  19.     }
  20.  
  21.     public void paint(Graphics g)
  22.     {
  23.         Font font = new Font("TimesRoman", Font.PLAIN, 24);
  24.         g.setFont(font);
  25.  
  26.         int height = font.getSize();
  27.  
  28.         if (badURL)
  29.             g.drawString("Bad URL!", 60, 130);
  30.         else
  31.         {
  32.             g.drawString("Type the URL to which", 25, 130);
  33.             g.drawString("you want to connect,",
  34.                 25, 130+height);
  35.             g.drawString("and then click the Connect",
  36.                 25, 130+height*2);
  37.             g.drawString("button.", 25, 130 + height*3);
  38.         }
  39.     }
  40.  
  41.     public boolean action(Event evt, Object arg)
  42.     {
  43.         String str = textField.getText();
  44.  
  45.         try
  46.         {
  47.             URL url = new URL(str);
  48.             AppletContext context = getAppletContext();
  49.             context.showDocument(url);
  50.         }
  51.         catch (MalformedURLException e)
  52.         {
  53.             badURL = true;
  54.             repaint();
  55.         }
  56.         
  57.         return true;
  58.     }
  59. }
  60.  
  61.