// File: Form1.java (Module 11) // // Author: Rahul Simha // Created: November 4, 1998. // // A simple form. import java.awt.*; import java.awt.event.*; import java.util.*; // For "Properties". import java.io.*; // For file I/O. class NewFrame extends Frame { // Data. Panel centerpanel; // For the questions. ScrollPane sc; // For placing the centerpanel. TextField lastname_tf; // To get the lastname. String lastname; TextField firstname_tf; // Firstname. String firstname; TextField networth_tf; // Net worth String networth; TextArea address_ta; // Address (TextArea) String address; // Constructor. public NewFrame (int width, int height) { this.setTitle ("Snoot Club Membership Form"); this.setResizable (true); this.setBackground (Color.white); this.setSize (width, height); // this.setLayout (new BorderLayout()); // First, a welcome message: Label L = new Label ("Please fill out the following form:"); 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)); // Each of the methods first_name() etc // will return a panel. centerpanel.add (first_name ()); centerpanel.add (last_name ()); centerpanel.add (net_worth ()); centerpanel.add (address ()); // Next, a scrollpane for the form. sc = new ScrollPane (); sc.add (centerpanel); this.add (sc, BorderLayout.CENTER); // Next, a panel of two buttons at the bottom. 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. submit (); } } ); 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); // Now add the panel to the frame. this.add (bottom_panel, BorderLayout.SOUTH); // Finally, show the frame. this.setVisible (true); } // No-parameter constructor. public NewFrame () { this (500, 300); } // Read in the first name. Panel first_name () { Panel subpanel = new Panel (); Label L = new Label ("First Name"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); firstname_tf = new TextField (20); firstname_tf.setForeground (Color.blue); firstname_tf.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { // Store in the string "firstname". firstname = firstname_tf.getText(); } } ); subpanel.add (firstname_tf); return subpanel; } // Get last name. Panel last_name () { Panel subpanel = new Panel (); Label L = new Label ("Last Name"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); lastname_tf = new TextField (20); lastname_tf.setForeground (Color.blue); lastname_tf.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { lastname = lastname_tf.getText(); } } ); subpanel.add (lastname_tf); return subpanel; } // Get net worth. Panel net_worth () { Panel subpanel = new Panel (); Label L = new Label ("Net worth (in millions)"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); networth_tf = new TextField (10); networth_tf.setForeground (Color.blue); networth_tf.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { networth = networth_tf.getText(); } } ); subpanel.add (networth_tf); return subpanel; } // Get address via a TextArea. Panel address () { Panel subpanel = new Panel (); Label L = new Label ("Address"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); // Default size: 4 lines, 30 chars wide. address_ta = new TextArea (4, 30); address_ta.setForeground (Color.blue); address_ta.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { address = address_ta.getText(); } } ); subpanel.add (address_ta); return subpanel; } // Process data when ready. void submit () { // Create a file by concatenating firstname and lastname. String filename = firstname + lastname; if (filename.length() == 0) return; // We will use a "Properties" instance to store data. Properties p = new Properties (); p.put ("firstname", firstname); p.put ("lastname", lastname); p.put ("networth", networth); p.put ("address", address); // Use the "save" feature of the Properties instance. try { FileOutputStream f = new FileOutputStream (filename); p.save (f, "Member information"); } catch (IOException e) { System.out.println (e); System.exit (0); } } } public class Form1 { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }