<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.awt.*;
import java.awt.event.*;
import java.io.*;

/* This class manages a window with 1 int field (numbered 0), 1 string field, 
 and a "ready" button. The user is
 expected to type values in fields and then click the "ready" button. When
 this button is clicked, method buttonPressed is called to process the values
 in the fields and deliver some output, possibly in the fields in the window.
 This version has been tailored to handle bowling-game scores
 */

public abstract class JLiveWindow {
    /** constants
     TOTAL_MAX_FIELDS: max number of fields of one type that is allowed
     MAX_FIELDS:       actual max number of fields used of one type. */
    private static final int TOTAL_MAX_FIELDS= 1;
    private static int MAX_FIELDS;
    private static final int MAX_FIELD_LENGTH= 20;
    private static final int ROW_SEPARATION= 10;
    private static final int COLUMN_SEPARATION= 15;
    
    private TextField[] intFields;     // the fields that can contain ints
    private TextField[] stringFields;  // the fields that can contain text
    private int numInts;               // number of int fields
    private int numStrings;            // number of text fields
    
    // The instructions for the game
    private TextArea instructions=    
        new TextArea("0: Read games from file given by user, copy them to the working set." +
             "\n1: Clear output area and then put the working set there." +
             "\n2: Sort the working set by total score." +
             "\n3: Sort the working set in reverse order of total score."       +  
             "\n4: Sort the working set alphabetically (by person's name)." +
             "\n5: Sort the working set in reverse alphabetical order (by person's name)." +
             "\n6: Append statistics of the working set (min, ave, max score) to the output area." +
             "\n7: Set the working set to a copy of all the games read in earlier." +
             "\n8: Remove from the working set games for the person named in first String field" +
             "\n9: Remove from the working set games with scores &lt; 200" +
             "\n\nAfter each command except 1 and 6, clear output area and put working set there;", 12, 10);
    // The output area
    protected TextArea output= new TextArea("Your output will appear here", 20, 60);
    
    // The window
    private java.awt.Frame frame = new java.awt.Frame("JLiveWindow");
    
    /** Constructor: a GUI window with 1 int field, 1 String field,
     a TextArea that gives instructions,
     a TextArea that shows results
     and a "ready" button */
    public JLiveWindow() {
        numInts= 1;
        numStrings= 1;
        MAX_FIELDS= 1;
        
        intFields= new TextField[MAX_FIELDS];
        stringFields= new TextField[MAX_FIELDS];
        
        createFields();
        instructions.setEditable(false);
        output.setEditable(false);
        output.setFont(new Font("Monospaced",Font.PLAIN,10));
        
        // Tell the program to exit upon closure of this window
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);}});
                
                // Add the Panel of fields and the button; the instruction textarea; and
                // the output textarea to this window
                frame.add("North", createPanel());
                frame.add("Center", instructions);
                frame.add("South", output);
    }
    
    // Create elements of arrays intFields, doubleFields, and stringFields
    private void createFields() {
        for(int i= 0; i&lt;MAX_FIELDS; i++) {
            intFields[i]= new TextField(MAX_FIELD_LENGTH);
            stringFields[i]= new TextField(MAX_FIELD_LENGTH);
        }
    }
    
    // Create and return the panel of fields that goes in this window
    private Panel createPanel() {
        Panel panel= new Panel();
        panel.setLayout(new GridLayout(1, 3, COLUMN_SEPARATION, 0));
        if (numInts &gt; 0)
            panel.add(createFieldsPanel(numInts,    intFields,    "Integer field"));
        if (numStrings &gt; 0)
            panel.add(createFieldsPanel(numStrings, stringFields, "String field"));
        panel.add(createActionButton());
        return panel;
    }
    
    // Create and return a Panel with visible elements a[0..n-1], invisible
    // elements a[n..MAX_FIELDS] (to make the vertical sizes nice), and with
    // title t above them.
    // Pre: n&gt;0
    private Panel createFieldsPanel(int n, TextField[] a, String t) {
        Panel panel= new Panel();
        panel.setLayout(new GridLayout(MAX_FIELDS+1, 1, 0, ROW_SEPARATION));
        if (n&gt;0)
            panel.add(new Label(t, Label.CENTER));
        for(int i= 0; i&lt;n; i++) {
            panel.add(a[i]);
        }
        // Add the invisible elements.
        for(int i= n+1; i&lt;MAX_FIELDS; i++) {
            panel.add(a[i]);
            a[i].setVisible(false);
        }
        return panel;
    }
    
    // Create and return a button with title "Ready"
    private Button createActionButton() {
        Button button= new Button("Ready!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonPressed();
            }});
            return button;
    }
    
    // Pack and show this window
    public void showWindow() {
        frame.pack();
        frame.setVisible(true);
    }
    
    // Process click of button titled "Ready!"
    abstract Object buttonPressed();
    
    // Return the integer in int field number f, or 0 if either f is not
    // valid or the field doesn't contain an integer.
    public int getIntField(int f) {
        try {
            return Integer.parseInt( intFields[f].getText() );
        } catch (ArrayIndexOutOfBoundsException e) {
            return 0;
        } catch (NumberFormatException e) {
            intFields[f].setText("" + intFields[f].getText() +": NOT AN INTEGER, 0 used");
            return 0;
        }
    }
    
    // Set int field number f to m.  No effect if f is not valid.
    public void setIntField(int f, int m) {
        try {
            intFields[f].setText("" + m);
        } catch (ArrayIndexOutOfBoundsException e) {
            return;
        }
    }
    
    // Return the string in string field number f, or "" if f is not valid.
    public String getStringField(int f) {
        try {
            return stringFields[f].getText();
        } catch (ArrayIndexOutOfBoundsException e) {
            return "";
        }
    }
    
    // Set string field number f to s.  No effect if f is not valid.
    public void setStringField(int f, String s) {
        try {
            stringFields[f].setText("" + s);
        } catch (ArrayIndexOutOfBoundsException e) {
            return;
        }
    }  
}
</pre></body></html>