// File: Form2.java (Module 11) // // Author: Rahul Simha // Created: November 4, 1998. // // A simple form with a menu import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; 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; MenuBar mb; // The menubar. // 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)); centerpanel.add (first_name ()); centerpanel.add (last_name ()); centerpanel.add (net_worth ()); centerpanel.add (address ()); // Next, the scrollpane. sc = new ScrollPane (); sc.add (centerpanel); this.add (sc, BorderLayout.CENTER); // The menubar mb = new MenuBar (); // Add a "File" menu with two items. Menu file_menu = new Menu ("File"); file_menu.add ("Load"); file_menu.add ("Quit"); file_menu.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { String item = a.getActionCommand (); if (item.equals ("Load")) load (); else if (item.equals ("Quit")) System.exit (0); } } ); mb.add (file_menu); // Add an "Action" menu with two items. Menu action_menu = new Menu ("Action"); action_menu.add ("Submit"); action_menu.add ("Clear"); action_menu.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { String item = a.getActionCommand (); if (item.equals ("Submit")) submit (); else if (item.equals ("Clear")) { firstname = ""; firstname_tf.setText (firstname); lastname = ""; lastname_tf.setText (lastname); networth = ""; networth_tf.setText (networth); address = ""; address_ta.setText (address); } } } ); mb.add (action_menu); // Note how a menubar is added to a Frame. this.setMenuBar (mb); // 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); } } // Load information from an existing Properties file. Properties info; void load () { // Create the filename. String filename = firstname + lastname; info = new Properties (); try { FileInputStream f = new FileInputStream (filename); info.load (f); // First and lastname are already entered. networth = info.getProperty ("networth"); networth_tf.setText (networth); address = info.getProperty ("address"); address_ta.setText (address); } catch (IOException e) { System.out.println ("No such file ... fatal error"); System.exit (0); } } } public class Form2 { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }