What is awt in Java

Chapter 1: Abstract Window Toolkit

Programming in Java: structure



This book is licensed under a Creative Commons license.


1.1 General

Developing user interfaces

The AWT - short for Abstract Window Toolkit - is a collection of classes for using graphic switching elements in a Java program. These button elements include, for example, combination buttons, check boxes or simple clickable buttons. With these classes it is possible to build up straightforward, clear user interfaces. Since the AWT has been part of the Java class hierarchy since the very first Java version 1.0, such graphical applications can run on all operating systems thanks to the portability of Java.

If you know a little about Java applets, you know that applets know a method. This can be overwritten by the programmer, for example to output texts at very specific positions in the applet surface. The output of text with or other drawing operations that can be performed in this method are more of the primitive access that can be quite sufficient for smaller applications. However, if you want to offer the user an interface that is as convenient as Windows applications, the AWT is just right for you.

Within the Java class hierarchy, the AWT classes are in their own package, namely in java.awt. If you want to create a user interface with the help of AWT classes, you should include all classes in this package.

This chapter explains the structure and use of the AWT from Java 1.1. The AWT in Java 1.0 is out of date and should no longer be used. In addition to the AWT, Java 1.2 offers a new, better class collection for developing graphical interfaces: The Swing Framework. Swing is a completely new approach and has nothing to do with AWT, but is only available from Java 1.2.


1.2 Overview

The AWT classes introduce themselves

The Java package java.awt consists of around 50 classes in Java 1.1. The most interesting classes are briefly presented below.

The most important classes for drawing buttons are as follows.

  • is a class for creating clickable buttons. OK and Cancel, as you know them from many Windows dialog boxes, are implemented in Java with exactly this class.

  • is a button that contains text. The user cannot perform any actions with this button. An object of this type is only a display option for text.

  • represents a one-line text entry field for the user.

  • represents a multi-line text input field for the user.

  • and are required if check boxes are to be created and grouped together. Check boxes are always used when the user is allowed to select exactly one of several options.

  • is a combo button.

  • presents the user with a selection option in the form of a list.

  • enables the development of pop-up menus. You are familiar with these menus from Microsoft Windows, if you click with the right mouse button, for example on the desktop - a pop-up menu then opens.

  • provides scroll bars. This class can also be used for other purposes, for example as a slider.

Auxiliary classes that support working with AWT classes are as follows.

  • has been used in many examples in the book Programming in Java: Introduction used. Whenever you need to work with colors, you can use this class. Many methods, for example for setting foreground and background colors, expect an object of this type as a parameter.

  • encapsulates the height and width of an element. So instead of passing height and width as separate values ​​to a method, for example, they can be stored in an object of this type, which is then passed as the only parameter.

  • is an auxiliary class that has two properties of type and is typically used to store coordinates.

The following classes are available in AWT for opening windows or otherwise working with windows.

  • creates windows with title bar and menu and can also have an icon as a window symbol. This class is the basis for developing ordinary windows.

  • is available to open a dialog box. A dialog window is really nothing more than a primitive surface on which buttons can be displayed. Dialog windows do not offer the option of showing title bars or menus. This class is therefore more suitable for short and concise user entries, for example for security queries when exiting an application.

  • represents a ready-made dialog window to search for files in the file system. This class is ideal when files are to be opened or saved.

The following classes are used to create menus.

  • represents a menu bar.

  • is a menu that opens in a menu bar.

  • is an entry within a menu.

  • is an entry within a menu that contains a check box.

  • is a key combination that can be linked to a menu item so that the corresponding menu item is activated by the key combination.

Java uses so-called layout managers to align buttons. The following layout managers are available in Java in the AWT. The purpose of layout managers is then explained in the chapter.

  • divides an area into the four cardinal points and a center.

  • creates a tab button. Such switching elements are suitable for accommodating a large number of buttons in a small space and for grouping them at the same time.

  • arranges buttons in sequence from left to right and from top to bottom - in the same way as a text flow. This class is the standard layout manager for Java applets.

  • arranges buttons in tabular form; when creating an object of this type, you can specify how many rows and columns the layout should have.

  • is the most complicated and at the same time the most flexible layout manager. Buttons are arranged in a table, whereby buttons can now also take the place of several fields in a row or column. Buttons no longer have a unit size as with.

Other interesting AWT classes are as follows.

  • embodies the ability to absorb other AWT classes. A class derived from can hold graphic buttons and draw them within their surface. A prominent class derived from is. A Java applet can therefore display buttons based on AWT classes in its surface.

  • provides access to the display of the mouse pointer. For example, it can be converted into a crosshair or a hand.

  • allows you to edit a font in order to then apply it to certain buttons.

  • manages graphics.

  • represents the empty space around a button - i.e. the edge in which no other button may be.

  • you will learn in Chapter 3, Graphics and sound know better. This class controls the loading of graphics. When the required graphics have been loaded, another class is informed so that the graphics can then be output in an applet, for example.

  • provides access to colors that are assigned by default by the computer system to various buttons such as menus. By taking the appropriate colors from this class, a Java application can use the same colors for the display of the buttons as the user has currently set for his computer system. This class is described in more detail in Chapter 6, System access presented.

In a separate chapter, this book presents in detail the use of swing classes in order to develop user interfaces with these classes, which have existed since Java 1.2. The classes presented above are not used in Swing with a few exceptions, so that it is unfortunately necessary to learn completely new classes for Swing.


1.3 Layout Manager

Arrange buttons correctly

The Java programming language follows the portability approach. Portable applications can run on a wide variety of devices. A portable application cannot assume that it has a very specific and fixed amount of display space. A portable application should try to make the most of the space available. Layout managers are the ideal tool for making the most of space for the display of buttons.

The class has a method. is derived from this class, among other things, so that you can also call this method for Java applets. With you can specify a layout manager that you want to use for your applet or your application. Depending on which layout manager you choose, buttons are arranged differently. By default, Java applets use the Layout Manager.

Consider the following example where three buttons are aligned using the standard Layout Manager in the Java applet.

import java.applet. *; import java.awt. *; public class MyApplet extends Applet {Button MyButton1, MyButton2, MyButton3; public void init () {MyButton1 = new Button ("MyButton 1"); MyButton2 = new Button ("MyButton 2"); MyButton3 = new Button ("MyButton 3"); add (MyButton1); add (MyButton2); add (MyButton3); }}

The Layout Manager inserts buttons line by line in the surface, and each centered. If the first line is full, the next button that is added with the container is packed into the next line - and of course centered again there. So it doesn't matter how much space the applet actually has on the website - the Layout Manager automatically arranges the buttons correctly in the space available.

divides the available space into five fields, which are assigned to the cardinal points and a center. If a button is now added to a container that uses the Layout Manager, when you call, you can specify in which of the five fields the button is to be placed.

import java.applet. *; import java.awt. *; public class MyApplet extends Applet {Button MyButton1, MyButton2, MyButton3; public void init () {setLayout (new BorderLayout ()); MyButton1 = new Button ("MyButton 1"); MyButton2 = new Button ("MyButton 2"); MyButton3 = new Button ("MyButton 3"); add (MyButton1, BorderLayout.WEST); add (MyButton2, BorderLayout.NORTH); add (MyButton3, BorderLayout.EAST); }}

A static constant of the class is passed to the method as a second parameter, which determines where the button is placed in the container. However, before the method is called for the first time, an object is transferred to the class and the layout manager is changed.

The Layout Managers, and are used similarly, but require a few more lines of Java code. They are not presented in more detail because the question arises as to how useful layout managers are. On the one hand, they have the advantage of automatically making optimal use of the available space. On the other hand, they draw a user interface that can hardly be described as beautiful and can hardly compete with clear windows from Windows applications.

Most developers assume that their Java applet will be given a very specific place on a website. If another user uses the Java applet in such a way that he allocates less space to it than the Java applet requires, this is his problem and not that of the developer. The likelihood that the Java applet will run on devices such as Java wristwatches, which due to their size can only provide very little space, is very low. The development of professional Java applets therefore normally takes place without a layout manager: buttons are aligned pixel-precisely. The resulting control over the structure of the user interface enables clear and elegant interfaces in which buttons do not compulsively try to usurp as much space as possible or to populate the interface in a unit size.


1.4 Pixel-precise placement

Arrange buttons to the point

A Layout Manager is no longer required to align buttons by specifying coordinates. You therefore call the following method to deactivate the standard layout manager for Java applets.

setLayout (null);

Passing the parameter to the method deactivates the Layout Manager. You now have to align the buttons yourself. The following methods are available for this.

  • defines the height and width of a button.

  • defines the coordinate at which the upper left corner of the button is to be located.

  • defines the coordinate for the upper left corner and at the same time the height and width of the button.

All three methods take parameters of the type. Instead, you can also pack the parameters in objects of the auxiliary class type, or, and then pass the corresponding object to the method as the only parameter in each case.

In the following example, three buttons are aligned with one another, each button being twice the size of the previous one.

import java.applet. *; import java.awt. *; public class MyApplet extends Applet {Button MyButton1, MyButton2, MyButton3; public void init () {setLayout (null); MyButton1 = new Button ("MyButton 1"); MyButton2 = new Button ("MyButton 2"); MyButton3 = new Button ("MyButton 3"); MyButton1.setBounds (10, 10, 100, 50); MyButton2.setBounds (10, 70, 200, 100); MyButton3.setBounds (10, 180, 400, 200); add (MyButton1); add (MyButton2); add (MyButton3); }}

Note that the call to to align the buttons occurs before the call to. So you can format buttons in memory in order to have them appear in the applet with the appropriate specifications. You should always carry out all the necessary steps to edit a button before placing the button on a surface.


1.5 User input

Something has to happen

A user interface only makes sense if it reacts to input from the user. After all, something has to happen when the user clicks a button. Some of the buttons work completely automatically. If you create a few check boxes and group them together, only one option can be activated at a time. If the user selects another option, the previous one is deselected - without you having to program it first. The following example illustrates this.

import java.applet. *; import java.awt. *; public class MyApplet extends Applet {Checkbox MyCheckbox1, MyCheckbox2, MyCheckbox3; CheckboxGroup MyCheckboxGroup; public void init () {setLayout (new GridLayout (3, 1)); MyCheckboxGroup = new CheckboxGroup (); MyCheckbox1 = new Checkbox ("MyCheckbox 1", MyCheckboxGroup, true); MyCheckbox2 = new Checkbox ("MyCheckbox 2", MyCheckboxGroup, false); MyCheckbox3 = new Checkbox ("MyCheckbox 3", MyCheckboxGroup, false); add (MyCheckbox1); add (MyCheckbox2); add (MyCheckbox3); }}

The above example uses the Layout Manager to create a table consisting of three rows and one column. Three objects of the type are placed in the three fields of the table. The three check boxes are combined into a group by creating an object of the type and passing this object as a second parameter to the constructor of. By the way, the third parameter in the constructor specifies whether the check box should initially be activated or not.

The example from above now also has a clickable button. While the class is well suited to representing such a graphical element, nothing happens when the user clicks the button. You have to program the corresponding action yourself.

import java.applet. *; import java.awt. *; import java.awt.event. *; public class MyApplet extends Applet implements MouseListener {Checkbox MyCheckbox1, MyCheckbox2, MyCheckbox3; CheckboxGroup MyCheckboxGroup; Button MyButton; public void init () {setLayout (new GridLayout (4, 1)); MyCheckboxGroup = new CheckboxGroup (); MyCheckbox1 = new Checkbox ("MyCheckbox 1", MyCheckboxGroup, true); MyCheckbox2 = new Checkbox ("MyCheckbox 2", MyCheckboxGroup, false); MyCheckbox3 = new Checkbox ("MyCheckbox 3", MyCheckboxGroup, false); MyButton = new Button ("MyButton"); MyButton.addMouseListener (this); add (MyCheckbox1); add (MyCheckbox2); add (MyCheckbox3); add (MyButton); } public void mouseReleased (MouseEvent ev) {Checkbox Active = MyCheckboxGroup.getSelectedCheckbox (); MyButton.setLabel (Active.getLabel ()); } public void mousePressed (MouseEvent ev) {} public void mouseEntered (MouseEvent ev) {} public void mouseExited (MouseEvent ev) {} public void mouseClicked (MouseEvent ev) {}}

In order to be able to react to user input, you have to intercept corresponding events. Depending on which action the user is performing, different events are triggered, i.e. different events occur. The java.awt.event package contains a number of interfaces and classes that are important in order to be able to react to user input. By including an interface and defining the appropriate methods, you prepare the class for certain events to occur. Each interface declares methods that are called automatically in very specific situations, for example when a mouse button is released. By defining the methods of the interface, you determine what should happen with user input.

The most interesting interfaces, including the methods they declare, are briefly presented below. The events for which the methods are called can be partly derived from their names.

  • is required to be able to react to keystrokes. The interface declares the three abstract methods, and. When the Java system calls all methods, a parameter of the type is transferred which contains further information on the event.

  • is an interface to be able to react to the maintenance and the loss of focus. That is, the two abstract methods and are called when a button has been selected or has lost the selection. One speaks here of the focus. A parameter of the type is transferred to both methods when the Java system is called.

  • responds to mouse events. The interface declares the five abstract methods,,, and. When the Java system calls all methods, a parameter of the type is transferred which contains further information on the event.

  • declares methods to react to mouse movements. The interface consists of the two abstract methods and. When the Java system is called, a parameter of the type is transferred to the methods, which contains further information on the event.

  • is an interface to react to text changes. It declares the abstract method to which a parameter of the type with further information on the event is transferred when the Java system is called.

  • use when you want to react to events affecting a window. This can be, for example, maximizing or closing a window. The interface declares the seven abstract methods,,,,, and. When the Java system calls all methods, a parameter of the type is transferred which contains further information on the event.

As you can see, the previous example implements the interface. Only the method has a method body that actually does something. The method is called automatically when a mouse button is released. The question is, where does the user actually have to click in order to release the mouse button and invoke the method? Finally, there are several buttons in the applet.

In order for the Java system to actually register and forward events, a corresponding method must be called for the object. For example, in order to be able to intercept mouse events for the button in the previous example, the method is called for the object. This method is defined in the class from which many AWT classes are derived. It is therefore not a problem for most buttons to call and thus react to mouse events on the respective button.

a parameter of the type must be passed - i.e. an object of a class that implements the interface. It is this object whose methods are called and which ultimately contains the appropriate instructions to respond to user input.

While with can be used to react to mouse events for an object, and are available to react to keyboard input, mouse movement and focus. These methods are also passed an object as a parameter, which the respective interface must implement and which contains the methods that are called when events occur.


1.6 Practical example

That's how it's done

In this section you will follow the step-by-step process of developing a small Java application. A Java applet with a button is to be displayed in the browser. If the user clicks on this, a Java window opens. The window presents the user with two text fields in which he can enter his height and weight. If he clicks on a button that is also located in the Java window, a message is issued whether the user has normal weight or is possibly over or underweight. The program uses the so-called body mass index, which is calculated by putting the body weight in relation to the body size squared.

Note that this is a sample application in Java that is intended to demonstrate the use of AWT classes. Do not use this sample application to draw medical conclusions in any way.

import java.applet. *; import java.awt. *; public class MyApplet extends Applet {Button Start; public void init () {setLayout (new BorderLayout ()); Start = new Button ("Start"); add (Start, BorderLayout.CENTER); }}

As a first step, a button is added to the Java applet that will later open the new Java window. The button should be displayed centered in the Java applet. For this purpose, the Layout Manager is set to, after which it makes centering very easy. Then the button is created with the help of the class and finally added with the applet.

The Java window, which should contain the input option for the user, is created with a second class in order to make the code clearer. The class gets the name because it is derived from the class.

import java.awt. *; public class MyFrame extends Frame {Label LabelSize, LabelWeight, LabelResult; TextField TextSize, TextWeight; Button OK; public MyFrame () {setLayout (new GridLayout (3, 2)); setSize (400, 120); LabelSize = new Label ("Your height in cm:"); LabelWeight = new Label ("Your weight in kg:"); LabelResult = new Label (); TextSize = new TextField (); TextWeight = new TextField (); OK = new button ("OK"); add (LabelSize); add (TextSize); add (LabelWeight); add (TextWeight); add (LabelResult); add (OK); }}

The class receives a single method: it has a name - just like the class itself - and has no return value. You are not even allowed to indicate. This method is a constructor. That is, this method is automatically called when an object of the type of this class is created with the keyword.

The Layout Manager is set to within the constructor. The surface of the window is now divided into a kind of table. How many rows and columns the table consists of is passed as a parameter to the constructor of the Layout Manager. Note that the first value defines the number of rows and the second value defines the number of columns.

Next, the size of the window is determined by calling. You should always call this method, even if you don't really care how big you want the window to be. If you do not call it, the window may be drawn so small that the user only sees the title bar - which can be a problem for inexperienced users and also does not look nice.

In the remaining lines of the constructor, various objects are created, assigned to the reference variables that are defined as properties of the class, and the objects are added above the window. The order in which the objects are transferred is decisive for the Layout Manager: The fields in the table are filled line by line from top left to bottom right.

Next, the class needs to be expanded so that a calculation is performed when the user enters height and weight and clicks the button. The interface is implemented for this purpose.

import java.awt. *; import java.awt.event. *; public class MyFrame extends Frame implements MouseListener {Label LabelSize, LabelWeight, LabelResult; TextField TextSize, TextWeight; Button OK; public MyFrame () {setLayout (new GridLayout (3, 2)); setSize (400, 120); LabelSize = new Label ("Your height in cm:"); LabelWeight = new Label ("Your weight in kg:"); LabelResult = new Label (); TextSize = new TextField (); TextWeight = new TextField (); OK = new button ("OK"); OK.addMouseListener (this); add (LabelSize); add (TextSize); add (LabelWeight); add (TextWeight); add (LabelResult); add (OK); } public void mouseReleased (MouseEvent ev) {if (TextSize.getText (). length> 0 && TextWeight.getText (). length> 0) {try {int Size = Integer.parseInt (TextSize.getText ()); int Weight = Integer.parseInt (TextWeight.getText ()); double BMI = Weight / ((Size / 100.0) * (Size / 100.0)); if (BMI <18.5) {LabelResult.setText ("You may be underweight."); } else if (BMI> 30) {LabelResult.setText ("You may be very overweight."); } else if (BMI> 25) {LabelResult.setText ("You may be overweight."); } else {LabelResult.setText ("You have normal weight."); }} catch (NumberFormatException ex) {}} else {LabelResult.setText ("Please enter your height and weight."); }} public void mousePressed (MouseEvent ev) {} public void mouseEntered (MouseEvent ev) {} public void mouseExited (MouseEvent ev) {} public void mouseClicked (MouseEvent ev) {}}

The implementation of means that the five abstract methods of the interface must be defined. In addition, some object - in this example the button - must be called in order to register it for mouse events.

The method includes the calculation for the body mass index. The result of this calculation is saved in the BMI variable. Depending on the result, a corresponding message is output directly in the window via the object of the type by calling the method. If an entry is missing, a message is displayed instead that the height and weight must be specified.

If you tried the above example, you would find that you cannot close the Java window. Neither the mouse click on a corresponding standard button in the title bar nor keyboard shortcuts cause the window to disappear. It may only help to close all browser windows until the Java window finally disappears.

So that the window is actually closed when the close button in the title bar is clicked, the class must implement the interface and specifically define the method. It is this method that is called when the user tries to close the window. In order for the window to be closed, this must be programmed within this method.

import java.awt. *; import java.awt.event. *; public class MyFrame extends Frame implements MouseListener, WindowListener {Label LabelSize, LabelWeight, LabelResult; TextField TextSize, TextWeight; Button OK; public MyFrame () {setLayout (new GridLayout (3, 2)); setSize (400, 120); LabelSize = new Label ("Your height in cm:"); LabelWeight = new Label ("Your weight in kg:"); LabelResult = new Label (); TextSize = new TextField (); TextWeight = new TextField (); OK = new button ("OK"); OK.addMouseListener (this); add (LabelSize); add (TextSize); add (LabelWeight); add (TextWeight); add (LabelResult); add (OK); addWindowListener (this); } public void mouseReleased (MouseEvent ev) {if (TextSize.getText (). length ()> 0 && TextWeight.getText (). length ()> 0) {try {int Size = Integer.parseInt (TextSize.getText () ); int Weight = Integer.parseInt (TextWeight.getText ()); double BMI = Weight / ((Size / 100.0) * (Size / 100.0)); if (BMI <18.5) {LabelResult.setText ("You may be underweight."); } else if (BMI> 30) {LabelResult.setText ("You may be very overweight."); } else if (BMI> 25) {LabelResult.setText ("You may be overweight."); } else {LabelResult.setText ("You have normal weight."); }} catch (NumberFormatException ex) {}} else {LabelResult.setText ("Please enter your height and weight."); }} public void mousePressed (MouseEvent ev) {} public void mouseEntered (MouseEvent ev) {} public void mouseExited (MouseEvent ev) {} public void mouseClicked (MouseEvent ev) {} public void windowClosing (WindowEvent ev) {setVisible (false) ; dispose (); } public void windowActivated (WindowEvent ev) {} public void windowClosed (WindowEvent ev) {} public void windowDeactivated (WindowEvent ev) {} public void windowDeiconified (WindowEvent ev) {} public void windowIconified (WindowEvent ev) {} public void windowOpened ( WindowEvent ev) {}}

In order to actually close the window, it is first hidden within the method with and then deleted from the memory with. So that the method is also called when the user wants to close the window, you must not forget to call it in the constructor.

The class is thus complete. What is still missing is the link to the class. So far, only one button has been created there, centered in the applet. So that the Java window that was developed in the class actually opens with a mouse click, the following is added.

import java.applet. *; import java.awt. *; import java.awt.event. *; public class MyApplet extends Applet implements MouseListener {Button Start; MyFrame Window; public void init () {setLayout (new BorderLayout ()); Start = new Button ("Start"); Start.addMouseListener (this); add (Start, BorderLayout.CENTER); Window = new MyFrame (); } public void mouseReleased (MouseEvent ev) {Window.setVisible (true); } public void mousePressed (MouseEvent ev) {} public void mouseEntered (MouseEvent ev) {} public void mouseExited (MouseEvent ev) {} public void mouseClicked (MouseEvent ev) {}}

The entire development of graphical applications with AWT classes is based on the concepts as you have learned about them in this chapter. You should now be clear about the way to go about creating user interfaces. For the work in practice, however, detailed documents on the individual AWT classes are essential. Only there you will find the exact definition of the innumerable methods that you can call for the AWT classes. No one can relieve you of familiarizing yourself with the classes and getting to know the AWT better. However, if you always keep this chapter in mind, you should overcome this hurdle without any problems.


1.7 Exercises

Practice creates masters

You can purchase the solutions to all of the exercises in this book as a ZIP file.

  1. Extend the example from Section 1.5, “User Inputs” so that the name of the selected check box is not only displayed in the button when the user clicks the mouse, but also when the button is activated via the keyboard.

    Check the documentation to find out which interface you need to respond to keystrokes on type buttons. It is not . Once you have found the right interface, you can not only expand the functionality of the applet, but also shorten the code considerably.

  2. Develop a Java applet that contains a combination button that allows the user to choose between the three colors red, green and blue.The selected color is set for the background of the applet.

    Use the class to represent a combination button. In order to catch an event when the user selects an entry from the combo button, you have to implement the interface.

  3. Develop a Java applet that includes a single line text entry box and a clickable type button. The text that is typed into the input element by the user is to be used as a label for the button and is to be set at the moment when the user presses the button with the mouse or keyboard.

    You need the classes and to create the user interface. Then use the interface to be informed when the button is pressed and to set the text entered by the user as a new label.

  4. Extend your solution to exercise 3 so that the new label is not only set when the type button is pressed, but also when the user presses Enter in the text input field.

    Use the interface to find out when the user presses Enter.

  5. Extend your solution to exercise 4 so that an additional button of the type indicates which control element currently has the focus.

    Use the interface to find out when a control gets and loses focus.

  6. Change your solution to exercise 5 so that you turn the Java applet into a Java application. Modify your application so that when the application starts, a window opens that contains the user interface that you developed for your Java applet in the previous tasks. Note that your application window actually closes when the user takes an appropriate action.

    To turn an applet into an application, add a public static method to your code that has the data type of the return value and expects a parameter of the type of an array of the type. Derive your class from, create an object of the type of your class in the method and call the methods and on the object to set the application window to a certain size and display it on the screen. Add the interface to your code to actually close the window if the user so wishes. As the last line of code of the method, add a call to the static method of the class to completely terminate the application when the window is closed.

  7. Develop a Java applet that contains a scroll bar through which the user can steplessly set the background color of the applet between black and red.

    To create a scroll bar, access the class. Use the interface to be informed when the user is using the scroll bar.

  8. Develop a Java applet in which you display three buttons of the type on each of four tabs of the Layout Manager. The four tabs, in turn, are designed to use the Layout Manager,, and so that the buttons are arranged in a different way on each tab. Since the Layout Manager does not automatically offer a switching element for changing tabs, you can also program a corresponding option.

    If the Layout Manager is used, all switching elements that are added with the container that uses the Layout Manager are viewed as individual tabs. To place three buttons of each type on a tab, create objects of type and add the three buttons to those objects. Then add the objects of the type to the container using the Layout Manager. Also, give the user a way to switch tabs, for example by creating four more buttons of the type outside of the tab control.

  9. Develop a Java applet whose surface you divide into four fields with the Layout Manager. Add an object of the type to each of the four fields and set the background color of the four panels to different values. When the mouse pointer is over the four panels, it should be changed and set anew for each panel. For example, change the cursor to crosshairs, a hand, a text insertion point, and an hourglass.

    All classes that are derived from - including - enable a mouse pointer to be set by calling the method, which is to be displayed automatically when the mouse is over the respective component. Call this method for your four panels and specify four different mouse pointers so that each panel has its own mouse pointer.

  10. Develop a Java applet that uses the Layout Manager and displays a single-line text entry field of the type centered. If the user enters "North", "South", "West", "East" or "Center" in the input field, the input field in the Layout Manager should be realigned. Use the interface to react to keystrokes in the input field. When entering the five terms for aligning the text input box in the applet interface, upper and lower case letters are not to be taken into account.

    To realign the text input field, you must first remove it from the applet surface with and then add it again with. When you call, you specify the new alignment of the text input field in the applet interface. Then call the method with which you force the applet to update the interface so that deleted buttons actually disappear with or newly added buttons become visible with.


Copyright © 2001-2010 Boris Schäling