// File: ex3.java (Module 11) // // Author: Rahul Simha // Created: November 10, 1998. // // Template for Ex. 11.3 // Using a List instead of a Choice in Q4 of the survey form. import java.awt.*; import java.awt.event.*; class NewFrame extends Frame { // Data. Panel centerpanel; // For the questions. CardLayout card; // For the centerpanel. // Constructor. public NewFrame (int width, int height) { this.setTitle ("Snoot Club Membership Test"); this.setResizable (true); this.setBackground (Color.cyan); this.setSize (width, height); // this.setLayout (new BorderLayout()); // First, the instruction line at the top. Label L = new Label ("Are you elitist enough \nfor our exclusive club?" + " Fill out the form and find out"); L.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 15)); L.setForeground (Color.blue); this.add (L, BorderLayout.NORTH); // Next, a panel of four buttons at the bottom. // The four buttons: quit, submit, next-question, previous-question. Panel bottom_panel = get_bottom_panel (); this.add (bottom_panel, BorderLayout.SOUTH); // Now, the center panel with the questions. centerpanel = new Panel (); // Set the layout to a CardLayout. card = new CardLayout (); centerpanel.setLayout (card); // Each question will be created in a separate method as // a panel. Note that the cardlayout requires a label as // second parameter. Panel p = first_question (); centerpanel.add (p, "1"); p = second_question (); centerpanel.add (p, "2"); p = third_question (); centerpanel.add (p, "3"); p = fourth_question (); centerpanel.add (p, "4"); // Now add the centerpanel to the frame. this.add (centerpanel, BorderLayout.CENTER); // Finally, show the frame. this.setVisible (true); } // No-parameter constructor. public NewFrame () { this (500, 300); } Panel get_bottom_panel () { // Create a panel into which we will place buttons. Panel bottom_panel = new Panel (); // A "previous-question" button. Button backward = new Button ("Previous question"); backward.setBackground (Color.green); backward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); backward.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { // Go back in the card layout. card.previous (centerpanel); } } ); bottom_panel.add (backward); // A forward button. Button forward = new Button ("Next question"); forward.setBackground (Color.green); forward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); forward.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { // Go forward in the card layout. card.next (centerpanel); } } ); bottom_panel.add (forward); // A submit button. Button submit = new Button ("Submit"); submit.setBackground (Color.green); submit.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); submit.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { // Perform submit task. compute_result(); } } ); bottom_panel.add (submit); Button quitb = new Button ("Quit"); quitb.setBackground (Color.red); quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); quitb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { System.exit (0); } } ); bottom_panel.add (quitb); return bottom_panel; } // This method will handle the panel for the first question. TextField tf; // Used in question 1. double q1_score = 0; // Score on question 1. Panel first_question () { // We will package everything into a panel and return the panel. Panel subpanel = new Panel (); // We will place things in a single column, so // a GridLayout with one column is appropriate. subpanel.setLayout (new GridLayout (8,1)); Label L1 = new Label ("Question 1:"); L1.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L1); Label L2 = new Label (" Select a vacation destination"); L2.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L2); Label L3 = new Label (" 1. Baltimore"); L3.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L3); Label L4 = new Label (" 2. Disneyland"); L4.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L4); Label L5 = new Label (" 3. Grand Canyon"); L5.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L5); Label L6 = new Label (" 4. French Riviera"); L6.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L6); Label L7 = new Label ("Enter 1,2,3 or 4 below:"); L7.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L7); // Here's the textfield to get user-input. tf = new TextField (); tf.addTextListener ( new TextListener () { // This interface has only one method. public void textValueChanged (TextEvent t) { String q1_string = tf.getText(); if (q1_string.equals ("2")) q1_score = 2; else if (q1_string.equals ("3")) q1_score = 3; else if (q1_string.equals ("4")) q1_score = 4; else q1_score = 1; } } ); subpanel.add (tf); return subpanel; } // For the second question, a collection of checkboxes // will be used. More than one selection can be made. // A listener is required for each checkbox. The state // of each checkbox is recorded. boolean // Store selections for this question. q2_option1, q2_option2, q2_option3, q2_option4; double q2_score; // Score on question 2. Panel second_question () { Panel subpanel = new Panel (); subpanel.setLayout (new GridLayout (7,1)); Label L1 = new Label ("Question 2:"); L1.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L1); Label L2 = new Label (" Select ONE OR MORE things that "); L2.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L2); Label L3 = new Label (" you put into your lunch sandwich"); L3.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L3); // Initialize the selections to false. q2_option1 = q2_option2 = q2_option3 = q2_option4 = false; // First checkbox. Checkbox c1 = new Checkbox ("Ham, beef or turkey"); c1.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); q2_option1 = c.getState(); } } ); subpanel.add (c1); // Second checkbox. Checkbox c2 = new Checkbox ("Cheese"); c2.addItemListener ( new ItemListener () { // This is where we will react to a change in checkbox. public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); q2_option2 = c.getState(); } } ); subpanel.add (c2); // Third checkbox. Checkbox c3 = new Checkbox ("Sun-dried Arugula leaves"); c3.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); q2_option3 = c.getState(); } } ); subpanel.add (c3); // Fourth checkbox. Checkbox c4 = new Checkbox ("Lemon-enhanced smoked Siberian caviar"); c4.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); q2_option4 = c.getState(); } } ); subpanel.add (c4); return subpanel; } // The third question allows only one among four choices // to be selected. We will use checkboxes, but also use // an associated CheckboxGroup to enforce only one selection. int q3_score = 0; Panel third_question () { Panel subpanel = new Panel (); subpanel.setLayout (new GridLayout (6,1)); Label L1 = new Label ("Question 3:"); L1.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L1); Label L2 = new Label (" And which mustard do you use?"); L2.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L2); // First, create the CheckboxGroup instance. // This instance will be passed to each Checkbox. CheckboxGroup cgroup = new CheckboxGroup(); // First checkbox. Checkbox c1 = new Checkbox ("Who cares?", cgroup, true); c1.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); if (c.getState()) q3_score = 1; } } ); subpanel.add (c1); // Second checkbox. Checkbox c2 = new Checkbox ("Safeway Brand", cgroup, false); c2.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); if (c.getState()) q3_score = 2; } } ); subpanel.add (c2); // Third checkbox. Checkbox c3 = new Checkbox ("Fleishman's", cgroup, false); c3.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); if (c.getState()) q3_score = 3; } } ); subpanel.add (c3); // Fourth checkbox. Checkbox c4 = new Checkbox ("Grey Poupon", cgroup, false); c4.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent i) { Checkbox c = (Checkbox) i.getSource(); if (c.getState()) q3_score = 4; } } ); subpanel.add (c4); return subpanel; } // For the fourth question we will use a drop-down Choice. double q4_score = 0; List list; Panel fourth_question () { Panel subpanel = new Panel (); subpanel.setLayout (new GridLayout (7,1)); Label L1 = new Label ("Question 4:"); L1.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L1); Label L2 = new Label (" Your movie preference, among these:"); L2.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L2); q4_score = 1; list = new List (); // INSERT YOUR CODE HERE for the list. subpanel.add (list); return subpanel; } // This method is called after submit is pressed. void compute_result () { // Clear the center panel. centerpanel.removeAll(); // Display a wait-cursor. this.setCursor (new Cursor(Cursor.WAIT_CURSOR)); // Create a new panel to display in the center. Panel subpanel = new Panel (new GridLayout (5,1)); // Score on question 1. Label L1 = new Label ("Score on question 1: " + q1_score); L1.setFont (new Font ("Serif", Font.ITALIC, 15)); subpanel.add (L1); // Score on question 2. if (q2_option1) q2_score += 1; if (q2_option2) q2_score += 2; if (q2_option3) q2_score += 3; if (q2_option4) q2_score += 4; q2_score = 0.6 * q2_score; Label L2 = new Label ("Score on question 2: " + q2_score); L2.setFont (new Font ("Serif", Font.ITALIC, 15)); subpanel.add (L2); // Score on question 3. Label L3 = new Label ("Score on question 3: " + q3_score); L3.setFont (new Font ("Serif", Font.ITALIC, 15)); subpanel.add (L3); // Score on question 4. Label L4 = new Label ("Score on question 4: " + q4_score); L4.setFont (new Font ("Serif", Font.ITALIC, 15)); subpanel.add (L4); // Weighted score. double avg = (q1_score + q2_score + q3_score + q4_score) / (double) 4; Label L5; if (avg <= 3.5) L5 = new Label ("Your average score: " + avg + " - REJECTED!"); else L5 = new Label ("Your average score: " + avg + " - WELCOME!"); L5.setFont (new Font ("Serif", Font.BOLD, 20)); L5.setAlignment (Label.CENTER); subpanel.add (L5); // Now add the new subpanel. centerpanel.add (subpanel, "5"); // Need to mark the centerpanel as "altered" centerpanel.invalidate(); // Go back to original cursor. this.setCursor (new Cursor(Cursor.DEFAULT_CURSOR)); // Everything "invalid" (e.g., the centerpanel above) // is now re-computed. this.validate(); } } public class ex3 { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }