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

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.net.*;
  4.  
  5. public class ConnectApplet3 extends Applet
  6. {
  7.     boolean badURL;
  8.  
  9.     public void init()
  10.     {
  11.         GridLayout layout = new GridLayout(2, 4, 10, 10);
  12.         setLayout(layout);
  13.  
  14.         Font font = new Font("TimesRoman", Font.PLAIN, 24);
  15.         setFont(font);
  16.  
  17.         Button button = new Button("Sun");
  18.         add(button);
  19.         button = new Button("Netscape");
  20.         add(button);
  21.         button = new Button("Microsoft");
  22.         add(button);
  23.         button = new Button("Macmillan");
  24.         add(button);
  25.         button = new Button("Time");
  26.         add(button);
  27.         button = new Button("CNet");
  28.         add(button);
  29.         button = new Button("Borland");
  30.         add(button);
  31.         button = new Button("Yahoo");
  32.         add(button);
  33.  
  34.         badURL = false;
  35.     }
  36.  
  37.     public void paint(Graphics g)
  38.     {
  39.         if (badURL)
  40.             g.drawString("Bad URL!", 60, 130);
  41.     }
  42.  
  43.     public boolean action(Event evt, Object arg)
  44.     {
  45.         String str;
  46.  
  47.         if (arg == "Sun")
  48.             str = "http://www.sun.com";
  49.         else if (arg == "Netscape")
  50.             str = "http://www.netscape.com";
  51.         else if (arg == "Microsoft")
  52.             str = "http://www.microsoft.com";
  53.         else if (arg == "Macmillan")
  54.             str = "http://www.mcp.com";
  55.         else if (arg == "Time")
  56.             str = "http://www.pathfinder.com";
  57.         else if (arg == "CNet")
  58.             str = "http://www.cnet.com";
  59.         else if (arg == "Borland")
  60.             str = "http://www.borland.com";
  61.         else
  62.             str = "http://www.yahoo.com";
  63.         try
  64.         {
  65.             URL url = new URL(str);
  66.             AppletContext context = getAppletContext();
  67.             context.showDocument(url);
  68.         }
  69.         catch (MalformedURLException e)
  70.         {
  71.             badURL = true;
  72.             repaint();
  73.         }
  74.         
  75.         return true;
  76.     }
  77. }
  78.  
  79.