Java Reference
In-Depth Information
9 Displaying Stock Information (continued)
4. In the constructor method Stock, set the background color to blue and
the foreground color to white so that the interface has white letters on a blue
background. Set the Next button's foreground color to black by typing
next.setForeground(Color.black); so that the button has black letters.
5. Type setLayout(new BorderLayout()); to set the frame's layout.
6. Type fieldPanel.setLayout(new GridLayout(4,2)); to set the data
panel layout.
7. Type buttonPanel.setLayout(new FlowLayout()); to set the button
area's layout.
8. Add the components and the ActionListener to the interface by entering the
methods from Table 8-17.
Table 8-17
CODE
fieldPanel.add(stockLabel);
fieldPanel.add(stock);
fieldPanel.add(volumeLabel);
fieldPanel.add(volume);
fieldPanel.add(priceLabel);
fieldPanel.add(price);
fieldPanel.add(changeLabel);
fieldPanel.add(change);
buttonPanel.add(next);
add(fieldPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
next.addActionListener(this);
9. In the try block of the actionPerformed() method, enter the code from
Table 8-18 to read the String data into the appropriate labels.
Table 8-18
CODE
stock.setText(input.readUTF());
volume.setText(input.readUTF());
price.setText(input.readUTF());
change.setText(input.readUTF());
10. When the end of the file is reached, a message should display and the fields
should be cleared. Enter the code from Table 8-19 in the catch block of the
actionPerformed() method.
Search WWH ::




Custom Search