Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^407


panel.add(amountField);
panel.add(enter);
panel.add(outputLabel);
} // End Station constructor

publicJPanel getPanel() // Instance method
{
returnpanel; // Return the filled panel
}

// Define an event handler with an instance of a Station
private classActionHandler implementsActionListener
{
public voidactionPerformed(ActionEvent event)
// Handle events from the Enter button in this station's panel
{
doublevalue; // Holds input value
// Convert string in amountField to a double value
value = Double.parseDouble(amountField.getText()); // Get value from field
total = total + value; // Add amount to sum
outputLabel.setText("Total Rainfall: "+ total); // Display total
amountField.setText("0.0"); // Clear input field
}
}
}


All that’s left is to design the application class and main. Given that so much of the
work is done in the Stationclass,mainhas very little to do. We need to declare and set
up our frame and content pane as usual. Then we instantiate as many Stationobjects
as there are stations, adding each one’s panel to the content pane. Finally, we display
the frame. Let’s assume there are three stations: Austin, Amherst, and LaCrosse.


public static void method main, takes the usual parameters
Declare a JFrame, inputFrame
Declare a Container, inputPane
Instantiate a JFrame for inputFrame
Set inputPane to the content pane of inputFrame
Set the size of inputFrame
Set the default closing operation for inputFrame to exit
Set the layout manager for inputPane to be an N by 1 grid
Instantiate station "Austin" and add its panel to inputPane
Instantiate station "Amherst" and add its panel to inputPane
Instantiate station "LaCrosse" and add its panel to inputPane
Make the frame visible
Free download pdf