Java AWT Components
Java's Abstract Windowing Toolkit provides many of the user interface objects we find in the Windows environment. These are called "Components" of the Java AWT. The applet below contains most of the components we will use to create a graphical user interface (GUI) for our applets. It simply initializes and creates the components but does not handle any of the events they trigger. Click on the hyperlink to the right of the component in order to see an example of the code that includes the event handlers.
|
This example simply places the components on the screen. It does not perform any actions other than the ones built in to the component. For example the buttons animate when you press them but this is simply because the component does that on its own. If you would like to see the code for this Applet click here and, if you like, copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "AWTcomp.java". But realize that the examples of the components and their use are in the hyperlinks in the table above.
After compiling this program we need to create an HTML page with a reference to the class file. It does not need a graphic window unless you are running it with an image or animation.
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute as below.
View of the applet's output
Now you can play with the buttons! These components of the AWT must be placed in some sort of 'container' within our applet. The Applet class is a subclass of the panel class. This is the reason why we can place these components within our applets. The next section on containers will finish the required concepts to allow us to program true Windows style GUI Applets and Applications.
Go to the next topic: AWT Containers