In this activity we complete the coding of the class FontDemo, the User Interface of which was developed in the activities for Unit 6. The interface is shown below.
We have provided the classes:
FontDemo is functionally identical to the class of the same name in the Unit 6 Activities but the code has been extended. That is, there are a few extra lines of code, some lines are taken out, and all the helper methods have been added.
You are asked to complete the coding of FontDemo so that
But don't worry, you don't have to know all about Java fonts and dialog boxes! We have provided two helper methods to handle the actual mechanics.
When the method updateFontSample is called, it will find out what font is wanted, and set the text sample accordingly. The method aboutDialog will display the required dialog.
Your job is to write the code that will cause the appropriate helper method to be invoked when an event occurs in the User Interface.
You will first need to write an inner class that implements ActionListener. You can name this class anything you please; we used FontDemoListener. It must of course have an actionPerformed(ActionEvent e) method, because otherwise it wouldn't implement ActionListener.
The actionPerformed(ActionEvent e) will have to find what the source of the event was (displayButton or aboutItem) and call updateFontSample() or aboutDialog as appropriate.
The second step will be to add listeners to the button and to the menu item.
(In practice you might write the code the other way around - begin by choosing a name for the listener class and adding the listeners where they are needed, then write the code for the listener class afterwards.)
From the previous coverage of packages, you might have expected java.awt.* to have included java.awt.event.*. However, java.awt.event.* is not included in the java.awt package.
All events inherit from the java.util.EventObject class and so have to be specifically imported into any program that needs to handle events.
This approach by the Java language may seem strange but it actually confers a great deal of flexibility - for example allowing you to define events that do not relate conceptually to GUI components.