// File: Survey1.java (Module 11) // // Author: Rahul Simha // Created: November 10, 1998. // // Step 2: using a textfield for the first question. 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; } // Second question. Panel second_question () { Panel subpanel = new Panel (); Label L1 = new Label ("Question 2:"); subpanel.add (L1); return subpanel; } // Third question. Panel third_question () { Panel subpanel = new Panel (); Label L1 = new Label ("Question 3:"); subpanel.add (L1); return subpanel; } // Fourth question. Panel fourth_question () { Panel subpanel = new Panel (); Label L1 = new Label ("Question 4:"); subpanel.add (L1); return subpanel; } // This method is called after submit is pressed. void compute_result () { // To be filled in ... } } public class Survey1 { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }