Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


408


Let’s call the application class Rainfall. Here is code for the complete application:

//***************************************************************************
// This application keeps track of rainfall amounts for three stations
//***************************************************************************
importjava.awt.*; // Layout manager
importjava.awt.event.*; // Event-handling classes
importjavax.swing.*; // User interface classes
importstation.*; // Rainfall station
public classRainfall
{
public static voidmain(String[] args)
{
JFrame inputFrame; // User interface frame
Container inputPane; // Content pane
// Set up the frame
inputFrame = newJFrame();
inputPane = inputFrame.getContentPane();
inputFrame.setSize( 500 , 150 );
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // End event loops
inputPane.setLayout(newGridLayout( 0 , 1 ));
// Add components to pane at start of event loops
// In each case, create a station object and
// directly get its panel to add to the frame
inputPane.add((newStation("Austin")).getPanel()); // Station for Austin
inputPane.add((newStation("Amherst")).getPanel()); // Station for Amherst
inputPane.add((newStation("LaCrosse")).getPanel()); // Station for LaCrosse
inputFrame.setVisible(true); // Show the frame
}
}

When the application executes, the frame shown below is displayed. The contents
shown in the frame are the result of entering several values, with the frame appearing
as it does just before the user clicks Enter to cause a value to be processed for the
Amherst station.
Free download pdf