> Interface > GUI construction > Graphical User Interfaces - Stage 2 | ||||||||
Graphical User Interfaces - Stage 2Now that we have covered the basics of creating a window frame and putting
single component in it. A reminder that this GUI code makes use of the AWT libraries in Java 1.1 and beyond. Make sure you have an appropriate Java version. The two components (Text field and button) will be placed on an AWT Panel This basic GUI simply creates a window with a button in it. When the button is clicked the music is computed and saved as both a MIDI and jMusic files. The GUI will look similar to this. Let's have a closer look.
Import the AWT and jm packages that are required.
The class declaration extends the Frame class AND implements the ActionListener.
The main() method simply calls the constructor method. This structure enables us to run the program or to call it from another.
The constructor method here is similar to the SimpleGUI_1 application
but adds features to create and use a Panel. In this code segment an new panel called 'p' is declared. As the TextField and Button are created they are added to the Panel. Notice that the TextField does not need to be registered with the actionListener, we are not concerned at what time the text is changed, the code grabs the text from the textField when it requires it. It can be changed at any time. The panel, which now contains a TextField and a Button is added to the Frame.
When the button is clicked this actionPerformed()
method is invoked.
The details of the makeMusic() method are not included here (see the source code), but suffice to say a jMusic score is created and saved as a MIDI file each time the 'Compose' button is clicked. Notice that the fileName argument in the method is passed to the writeMIDI method as the SMF filename. |
||||||||