Source of WebSitesApplet.java


  1: // WebSitesApplet.java
  2: // Applet to display a set of buttons on the screen that
  3: // allow the user to access a selection of web sites.

  5: import java.awt.*;
  6: import java.awt.event.*;
  7: import java.applet.*;
  8: import java.net.*;

 10: public class WebSitesApplet
 11:     extends Applet
 12:     implements ActionListener
 13: {
 14:     // Initialize an array with the names of the Web sites
 15:     String[] webSiteNames =
 16:         {"Oxford Brookes University",
 17:          "Oxford Computing Laboratory",
 18:          "Jones & Bartlett",
 19:          "Java Development",
 20:          "Windows 95 utilities"};

 22:     // Initialize an array with the corresponding URLs of those Web sites
 23:     String[] webURLs =
 24:         {"http://www.brookes.ac.uk",
 25:          "http://www.comlab.ox.ac.uk",
 26:          "http://www.jbpub.com",
 27:          "http://java.sun.com",
 28:          "http://www.windows95.com"};

 30:     // Array of buttons that will denote the different Web sites
 31:     Button[] button = new Button[webSiteNames.length];

 33:     // Override method init to display the group of buttons
 34:     public void init()
 35:     {
 36:         setLayout(null);
 37:         setBackground(Color.yellow);
 38:         for (int index=0; index!=webSiteNames.length; index++)
 39:         {
 40:             button[index] = new Button(webSiteNames[index]);
 41:             button[index].setLocation(10, 25*index);
 42:             button[index].setSize(200, 20);
 43:             button[index].setBackground(Color.cyan);
 44:             button[index].addActionListener(this);
 45:             add(button[index]);
 46:         }
 47:     }

 49:     // Implement the actionPerformed method
 50:     // in the ActionListener interface
 51:     public void actionPerformed(ActionEvent event)
 52:     {
 53:         // Find the name of the button that was pressed
 54:         Object source = event.getActionCommand();
 55:         for (int index=0; index != webSiteNames.length; index++)
 56:         {
 57:             // Inspect each web site name in the array of Web site names
 58:             if (source.equals(webSiteNames[index]))
 59:             {
 60:                 // Display the relevant page from the Web site
 61:                 // if and when a match is found
 62:                 try
 63:                 {
 64:                     URL site = new URL(webURLs[index]);
 65:                     getAppletContext().showDocument(site);
 66:                 }
 67:                 catch (MalformedURLException m)
 68:                 {
 69:                     System.exit(1);
 70:                 }
 71:                 return;
 72:             }
 73:         }
 74:     }
 75: }