// this version works with Java 1.0 (e.g. VJ++ 1.1) // oct-16-1998 import java.awt.*; import java.applet.*; import java.util.*; import Set; public class SetAppletV10 //extends Frame // use Frame for stand-alone extends Applet // use Applet for applet { /** public static void main(String[] argv) // use main for stand-alone { Frame f = new SetAppletV10(); f.resize(400,300); f.show(); } // main **/ public void init() // use init for applet // public SetAppletV10() // use constructor for stand-alone { set1 = new Set(); set2 = new Set(); // components setLayout(new BorderLayout()); // checkbox group selectorGroup = new CheckboxGroup(); // checkboxes set1Selector = new Checkbox("Set 1",selectorGroup,true); set2Selector = new Checkbox("Set 2",selectorGroup,false); 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:"); value = new TextField(5); clear = new Button("Clear"); union = new Button("Union"); intersect = new Button("Intersect"); p = new Panel(); p.add(add); p.add(value); p.add(clear); p.add(union); p.add(intersect); add("South", p); // set active Checkbox activeSet = set1; activeList = set1List; } // init // handles actions generated by buttons and checkboxes public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } // handleEvent public boolean action(Event ae, Object arg) { if (ae.target.equals(add)) { addElement(); repaint(); } else if (ae.target.equals(union)) // union two sets loadResult(set1.union(set2)); else if (ae.target.equals(intersect)) loadResult(set1.intersection(set2)); else if (ae.target.equals(clear)) { activeSet.removeAllElements(); activeList.clear(); } else if (ae.target.equals(set1Selector)) { activeList = set1List; activeSet = set1; } else if (ae.target.equals(set2Selector)) { activeList = set2List; activeSet = set2; } else return super.action(ae,arg); return true; } // action private void addElement() { int i = Integer.parseInt(value.getText()); activeSet.addElement(new Integer(i)); // ours is an Integer set activeList.addItem(Integer.toString(i)); // list only takes String } // addElement private void loadResult(Set resultSet) { resultList.clear(); Enumeration e = resultSet.elements(); while (e.hasMoreElements()) resultList.addItem(e.nextElement().toString()); repaint(); } // instance variables Button add, union, intersect, clear; List set1List, set2List, activeList, resultList; Checkbox set1Selector, set2Selector; CheckboxGroup selectorGroup; TextField value; Set set1, set2, activeSet; } // SetApplet