// File: TestScrollPane.java (Module 11) // // Author: Rahul Simha // Created: November 10, 1998. // // Replaces the Cardlayout with a single Panel // and a ScrollPane. import java.awt.*; import java.awt.event.*; class NewFrame extends Frame { // Data. Panel centerpanel; // For the questions. TextField tf; // Used in question 1. boolean // Store selections for Q2. q2_option1, q2_option2, q2_option3, q2_option4; Choice q4_choice; // For question 4. double // Score on each question. q1_score = 0, q2_score = 0, q3_score = 0, q4_score = 0; ScrollPane sc; // For placing all the questions. // 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, a welcome message, as a Label. Label L = new Label ("Are you elitist enough for 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); // Now the center panel. centerpanel = new Panel (); centerpanel.setLayout (new GridLayout (4,1)); centerpanel.add (first_question ()); centerpanel.add (second_question()); centerpanel.add (third_question()); centerpanel.add (fourth_question()); // Next, the scrollpane. sc = new ScrollPane (); sc.add (centerpanel); // Add the scrollpane to the frame. this.add (sc, BorderLayout.CENTER); // 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); // Finally, show the frame. this.setVisible (true); } // No-parameter constructor. public NewFrame () { this (500, 300); } // The first question uses labels for the question and // gets input via a textfield. A panel containing all // these things is returned. The question asks for // a vacation destination: the more exotic the location, // the higher the score. 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. 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. 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. Panel fourth_question () { Panel subpanel = new Panel (); subpanel.setLayout (new GridLayout (3,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); // Create a Choice widget. q4_choice = new Choice (); q4_choice.add ("Lethal Weapon IV"); q4_choice.add ("Titanic"); q4_choice.add ("Saving Private Ryan"); q4_choice.add ("Le Art Movie avec subtitles"); q4_score = 1; q4_choice.addItemListener ( new ItemListener () { public void itemStateChanged (ItemEvent e) { q4_score = 1 + q4_choice.getSelectedIndex(); } } ); subpanel.add (q4_choice); return subpanel; } 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(); } Panel get_bottom_panel () { // Create a panel into which we will place buttons. Panel bottom_panel = new Panel (); // 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; } } public class TestScrollPane { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }