Java Reference
In-Depth Information
the form, use them as arguments to the currency conversion process, and then
return the result of the conversion to the user in the Results field.
Swing's event model says that when a button is clicked, an ActionEvent is fired
and passed through the queue of registered ActionListeners . In the constructor
of the class, shown in listing 10.6, you implement this feature by defining an
anonymous inner class of type ActionListener .
Listing 10.6
Implementation of the Convert button's functionality
...
convertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fromCurrStr = (String)fromCurrency.getSelectedItem();
String toCurrStr = (String)toCurrency.getSelectedItem();
String amountStr = amountField.getText();
DecimalFormat df = new DecimalFormat("0.00");
try {
double amount = Double.parseDouble(amountStr);
CurrencyExchangeService service =
CurrencyExchangeServiceFactory.getService();
double rate = service.requestCurrentRate
(fromCurrStr, toCurrStr);
resultField.setText(df.format(amount * rate));
} catch (NumberFormatException e1) {
resultField.setText(
"The amount specified was not a valid number.");
}
}
});
...
10.6.4
Providing an entry point
Finally, you require an entry point: Somewhere in the system, a class has to be
responsible for creating a new frame or window and placing the form in it for the
user to see. This same class is typically responsible for proper disposal of the frame.
You can implement that functionality in a main() method, as shown in listing 10.7.
Listing 10.7 main() method that gives a launching point for running the GUI
public static void main(String[] args) {
ACMEGUI acmeGui = new ACMEGUI();
 
 
 
 
 
Search WWH ::




Custom Search