// chap_11\netlinks.java // program to display a set of buttons on the screen // to allow for the selection of different sites on the Web import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; public class netlinks extends Applet implements ActionListener { // initialize an array with the names of the Web sites String[] webSiteNames = {"Oxford Brookes University", "Oxford Computing Laboratory", "Jones & Bartlett", "Java Development", "Windows 95 utilities"}; // initialize an array with the URLs of the Web sites String[] webURLs = {"http://www.brookes.ac.uk", "http://www.comlab.ox.ac.uk", "http://www.jbpub.com", "http://java.sun.com", "http://www.windows95.com"}; // array of buttons that will denote the different Web sites Button[] button = new Button[webSiteNames.length]; // override method init to display the group of buttons public void init() { setLayout(null); setBackground(Color.yellow); for (int index=0; index != webSiteNames.length; index++) { button[index] = new Button(webSiteNames[index]); button[index].setLocation(10,25*index); button[index].setSize(200,20); button[index].setBackground(Color.cyan); button[index].addActionListener(this); add(button[index]); } } // use an implementation of the actionPerformed method taken // from the ActionListener abstract class public void actionPerformed(ActionEvent event) { // find the name of the button that was pressed Object source = event.getActionCommand(); for (int index=0; index != webSiteNames.length; index++) { // inspect each web site name in the array of Web site names if (source.equals(webSiteNames[index])) { // display the relevant page from the Web site try { URL site = new URL(webURLs[index]); getAppletContext().showDocument(site); } catch (MalformedURLException m){System.exit(1);} return; } } } }