Lesson FourteenUnderstanding the Abstract Windows Toolkit (AWT)In Lesson Eight, you learned a little about the Abstract Windows Toolkit (AWT) when you created you applets. Now, combining inheritance with the AWT, we will be able to create more interactive programs. This is what we call a graphical user interface (GUI). All of the programs we will be creating has methods that come from the Component class. Two of these methods are setSize(), which allows you to set the size of any component, and setVisible(), which allows you to make a component visible or invisible to the user. Note: these programs are applications, not applets, but these can be added to applets. Let's look at the code for a simple Frame: Setting true to the setVisible() method makes the window appear. To remove the window, set the value to false. This Frame works just like every other Frame you've used with the exception of that this Frame won't close. The reason for this is that you may want to add a closing event like closing a file, disable buttons, etc. For this, you have a method called windowClosing(). To do this, you would add: You use a "0" in the exit() statement to signal that the program will close normally. There are six other methods that must be included in the programs: Now that we have a general idea of what is included in the creation of a Frame, we can create a Frame that we can close. Below are two programs that will allow us to do that. Now that we have created a program that will allow us to close the Frame window, we can do just about anything. But this seems like a lot of work to do everytime you want to create a Frame. For this, we will use the WindowAdapter class. Classes that are adapter classes implement an abstract class and provide the required methods for all of the abstract class' methods. In "FrameYouCanClose.java", eliminate FrameYouCanClose() method and everything after the windowClosing() method except the closing curly bracket. Save this program as "WindowYouCanClose.java". Make sure that this program extends WindowAdapter instead of Frame and remove the "implements WindowListener". Go back to "FrameYouCanClose.java" and rename it as "FrameYouCanClose2.java". This can still extend Frame, but remove the "implements WindowListener" from here also. Rename the FrameYouCanClose() method "FrameYouCanClose2()". In the addWindowListener(this), change "this" to "new WindowYouCanClose()". Then, go to "DemoClosingFrame.java", rename it "DemoClosingFrame2.java", then change all the "FrameYouCanClose" to "FrameYouCanClose2". Next we'll create an applet that uses the FrameYouCanClose objects. Note: Depending upon the version of Java you are using, you might see a "Warning: AppletWindow" message in your applet. Ignore this message. Question: I thought a graphical user interface was more like a form. Where are the other objects besides the Label and Button objects? Answer: You are learning too quick. It's almost like you are using telepathy. Next we'll learn about the rest of the components.
If you have a questions about any of the lessons, feel free to ask. |