//------------// Introduction to Programming Using Java: An Object-Oriented Approach//	Arnow/Weiss//------------//------------// Chapter 8 / GUI Supplement / Page 305//	An applet using the Set class//------------ //------------// Notes//	- This applet may be run by opening the file 'SetApplet.html'//		in a browser or appletviewer.//------------import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.*;public class SetApplet extends Applet 						implements ActionListener, 									ItemListener {	public void init() {		set1 = new Set();		set2 = new Set();		// Components		setLayout(new BorderLayout());		// Checkboxes		set1Selector = new Checkbox("Set 1");		set1Selector.addItemListener(this);   		set2Selector = new Checkbox("Set 2");		set2Selector.addItemListener(this);		selectorGroup = new CheckboxGroup();		set1Selector.setCheckboxGroup(selectorGroup);		set2Selector.setCheckboxGroup(selectorGroup);		Panel p = new Panel();		p.add(set1Selector);		p.add(set2Selector);		add("North", p);		// Lists		set1List = new List();		set1List.setBackground(Color.white);		set2List = new List();		set2List.setBackground(Color.white);		resultList = new List();		resultList.setBackground(Color.white);		p = new Panel();		p.add(new Label("Set1"));		p.add(set1List);		p.add(new Label("Set2"));		p.add(set2List);		p.add(new Label("Result"));		p.add(resultList);	  	add("Center", p);		// Buttons		add = new Button("Add:");		add.addActionListener(this);		value = new TextField(5);		clear = new Button("Clear");		clear.addActionListener(this);		union = new Button("Union");		union.addActionListener(this);		intersect = new Button("Intersect");		intersect.addActionListener(this);		p = new Panel();		p.add(add);		p.add(value);		p.add(clear);		p.add(union);		p.add(intersect);		add("South", p);		// Set active check box		selectorGroup.setSelectedCheckbox(set1Selector);		setActive();	}	// Handles action events generated by the buttons	public void actionPerformed(ActionEvent ae) {		if (ae.getSource() == add) {			addElement();			repaint();		}		else if (ae.getSource() == union)			loadResult(set1.union(set2));		else if (ae.getSource() == intersect)			loadResult(set1.intersection(set2));		else if (ae.getSource() == clear) {			activeSet.removeAllElements();			activeList.removeAll();		}	}	// Handles item events generated by the check boxes	public void itemStateChanged(ItemEvent ie) {		setActive();	}	private void addElement() {		int i = Integer.parseInt(value.getText());		activeSet.addElement(new Integer(i));		activeList.addItem(Integer.toString(i));	}  private void setActive() {		List newActiveList;		if (selectorGroup.getSelectedCheckbox() == set1Selector) {			activeList = set1List;			activeSet = set1;		}		else {			activeList = set2List;			activeSet = set2;		}	}	private void loadResult(Set resultSet) {		resultList.removeAll();		Enumeration e = resultSet.elements();		while (e.hasMoreElements())			resultList.addItem(e.nextElement().toString());		repaint();	}	Button add, union, intersect, clear;	List set1List, set2List, activeList, resultList;	Checkbox set1Selector, set2Selector;	CheckboxGroup selectorGroup;	TextField value;	Set set1, set2, activeSet;}