/* Creates a practice user interface, consisting of several text, checkbox, button, and menu elements. */ import java.awt.*; import java.applet.*; public class sherlockianUI extends Applet { GridBagLayout thisLayout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); //declare user input & interface items TextField inputField; String username="Your Name"; Label l_0, l_1, l_2, l_3, l_4, l_5; Checkbox elist0, elist1, elist2, favChar0, favChar1, favChar2, favChar3; CheckboxGroup characters; Choice villainMenu; List wives, wivesChosen; TextArea commentField; Button updateB; public void init() { setBackground(Color.white); this.setLayout(thisLayout); /* This uses GridBagLayout to define certain layout constraints--i.e., features of the screen elements such as their x,y positions on a grid; the top, left, bottom, and right margins around each element; the width and height in grid cells that elements can take up; whether the elements can grow into or "fill" more grid cells if space is available; and their relative priority or "weight" in filling up space. */ c.fill = GridBagConstraints.BOTH; c.insets = new Insets(5,5,5,5); c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0.0; c.weighty = 0.0; l_0 = new Label("Name: (press enter afterward)"); thisLayout.setConstraints(l_0, c); add(l_0); c.gridy = 1; inputField = new TextField(20); thisLayout.setConstraints(inputField, c); add(inputField); c.gridx = 1; c.gridy = 0; l_1 = new Label("Check any or all that apply:"); thisLayout.setConstraints(l_1, c); add(l_1); c.gridy= 1; elist0 = new Checkbox("Hound of the Internet", null, false); thisLayout.setConstraints(elist0, c); add(elist0); c.gridy = 2; elist1 = new Checkbox("Baker Street E-regular", null, false); thisLayout.setConstraints(elist1, c); add(elist1); c.gridy = 3; elist2 = new Checkbox("Russ-l list member", null, false); thisLayout.setConstraints(elist2, c); add(elist2); c.gridx = 2; c.gridy = 0; l_2 = new Label("Favorite Character:"); thisLayout.setConstraints(l_2, c); add(l_2); //a CheckboxGroup, unlike the independent Checkboxes above, //only allows one item to be selected at a time. c.gridy = 1; characters = new CheckboxGroup(); favChar0 = new Checkbox("Sherlock Holmes", characters, false); thisLayout.setConstraints(favChar0, c); add(favChar0); c.gridy = 2; favChar1 = new Checkbox("John H. Watson", characters, false); thisLayout.setConstraints(favChar1, c); add(favChar1); c.gridy = 3; favChar2 = new Checkbox("Mrs. Hudson", characters, false); thisLayout.setConstraints(favChar2, c); add(favChar2); c.gridy = 4; favChar3 = new Checkbox("Mycroft Holmes", characters, false); thisLayout.setConstraints(favChar3, c); add(favChar3); c.gridx = 0; c.gridy = 2; l_3 = new Label("Best Villain:"); thisLayout.setConstraints(l_3, c); add(l_3); c.gridy = 3; villainMenu = new Choice(); villainMenu.addItem("Professor Moriarty"); villainMenu.addItem("Colonel Sebastian Moran"); villainMenu.addItem("Charles Augustus Milverton"); villainMenu.addItem("John Clay"); villainMenu.addItem("Baron Adelbert Gruner"); villainMenu.addItem("Dr. Grimesby Roylott"); villainMenu.addItem("Jephro Rucastle"); thisLayout.setConstraints(villainMenu, c); add(villainMenu); //Two scrolling lists will be created, so that the user may //select or deselect their choices from the first list. c.gridy = 4; l_4 = new Label("Choose Watson's possible wives:"); thisLayout.setConstraints(l_4, c); add(l_4); c.gridy = 5; wives = new List(3, true); wives.addItem("Constance Adams"); wives.addItem("anonymous 1st wife"); wives.addItem("Mary Morstan"); wives.addItem("anonymous 2nd wife"); wives.addItem("anonymous 3rd wife"); thisLayout.setConstraints(wives, c); add(wives); //This is the second, blank scrolling list, to which items //can be added by the user. See handleEvent method below. c.gridx = 1; wivesChosen = new List(3, true); thisLayout.setConstraints(wivesChosen, c); add(wivesChosen); c.gridx = 0; c.gridy = 6; l_5 = new Label("Comments or Suggestions:"); thisLayout.setConstraints(l_5, c); add(l_5); c.gridx = 1; c.gridheight = 2; commentField = new TextArea(4, 25); thisLayout.setConstraints(commentField, c); add(commentField); c.gridx = 2; c.gridheight = 1; updateB = new Button("All Done"); thisLayout.setConstraints(updateB, c); add(updateB); } public boolean action(Event e, Object arg) { String thanks; Graphics g = getGraphics(); thanks = "Thanks, " + username + "!"; if (e.target instanceof Button) { Font f = new Font("TimesRoman", Font.BOLD, 24); g.setFont(f); g.drawString(thanks, 180, 390); return true; } if (e.target instanceof TextField) { username = inputField.getText(); return true; } return false; } // end of action method public boolean handleEvent(Event e) { String[] chosen; if (e.id == Event.LIST_SELECT || e.id == Event.LIST_DESELECT) { chosen = wives.getSelectedItems(); wivesChosen.clear(); for (int i=0; i-->