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

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.net.*;
  4.  
  5. public class ConnectApplet2 extends Applet
  6. {
  7.     boolean badURL;
  8.  
  9.     public void init()
  10.     {
  11.         GridLayout layout = new GridLayout(2, 2, 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.  
  26.         badURL = false;
  27.     }
  28.  
  29.     public void paint(Graphics g)
  30.     {
  31.         if (badURL)
  32.             g.drawString("Bad URL!", 60, 130);
  33.     }
  34.  
  35.     public boolean action(Event evt, Object arg)
  36.     {
  37.         String str;
  38.  
  39.         if (arg == "Sun")
  40.             str = "http://www.sun.com";
  41.         else if (arg == "Netscape")
  42.             str = "http://www.netscape.com";
  43.         else if (arg == "Microsoft")
  44.             str = "http://www.microsoft.com";
  45.         else
  46.             str = "http://www.mcp.com";
  47.  
  48.         try
  49.         {
  50.             URL url = new URL(str);
  51.             AppletContext context = getAppletContext();
  52.             context.showDocument(url);
  53.         }
  54.         catch (MalformedURLException e)
  55.         {
  56.             badURL = true;
  57.             repaint();
  58.         }
  59.         
  60.         return true;
  61.     }
  62. }
  63.  
  64.