AWT GUI Components Implementing the Graphical User Interface

J.E.D.I

5.6 Implementing the Graphical User Interface

Without learning about graphical user interface GUI APIs, you would still be able to create quite a descent range of different programs. However, your applications are very likely to be bland and unappealing to the users. Having a good GUI affects the usage of your application. This results in ease of use and enjoyment of use for the users of your program. Java provides tools like Abstract Windowing Toolkit AWT and Swing to develop interactive GUI applications. The following section provides a discussion on the Abstract Windowing Toolkit and Swing 8 . The Java Foundation Classes JFCs, which is an important part of the Java SDK, refers to a collection of APIs that simplifies the development Java GUI applications. It primarily consists of five APIs including AWT and Swing. The three other APIs are Java2D, Accessibility, and Drag and Drop. All these APIs assist developers in designing and implementing visually-enhanced applications. Both AWT and Swing provides GUI components that can be used in creating Java applications and applets. You will learn about applets in a latter section. Unlike some AWT components that use native code, Swing is written entirely using the Java programming language. As a result, Swing provides a platform-independent implementation ensuring that applications deployed across different platforms have the same appearance. AWT, however, does ensure that the look and feel of an application run on two different machines be comparable. The Swing API is built around a number of APIs that implement various parts of the AWT. As a result, AWT components can still be used with Swing components.

5.6.1 AWT GUI Components

Fundamental Window Classes In developing GUI applications, the GUI components such as buttons or text fields are placed in containers. These are the list of important container classes provided in the AWT. 8 The use of the text verbatim from the JEDI Introduction to Programming II Course has prior approval from their authors. Software Engineering 220 J.E.D.I AWT Class Description Component An abstract class for objects that can be displayed on the console and interact with the user. The root of all other AWT classes. Container An abstract subclass of the Component class. A component that can contain other components. Panel Extends the Container class. A frame or window without the titlebar, the menubar nor the border. Superclass of the Applet class. Window Also extends Container class. A top-level window, which means that it cannot be contained in any other object. Has no borders and no menubar. Frame Extends the Window class. A window with a title, menubar, border, and resizing corners. Has four constructors, two of which have the following signatures: Frame FrameString title Table 1.2.1: AWT Container classes To set the size of the window, the overloaded setSize method is used. void setSizeint width, int height Resizes this component to the width and height provided as parameters. void setSizeDimension d Resizes this component to d.width and d.height based on the Dimension d specified. A window by default is not visible unless you set its visibility to true. Here is the syntax for the setVisible method. void setVisibleboolean b In designing GUI applications, Frame objects are usually used. Heres an example of how to create such an application. import java.awt.; public class SampleFrame extends Frame { public static void mainString args[] { SampleFrame sf = new SampleFrame; sf.setSize100, 100; Try removing this line sf.setVisibletrue; Try removing this line } } Note that the close button of the frame doesnt work yet because no event handling mechanism has been added to the program yet. Youll learn about event handling in the next module. Graphics Several graphics method are found in the Graphics class. Here is the list of some of these methods. Software Engineering 221 J.E.D.I drawLine drawPolyline setColor fillRect drawPolygon getFont drawRect fillPolygon setFont clearRect getColor drawString Table 1.2.2a: Some methods of class Graphics Related to this class is the Color class, which has three constructors. Constructor Format Description Colorint r, int g, int b Integer value is from 0 to 255. Colorfloat r, float g, float b Float value is from 0.0 to 1.0. Colorint rgbValue Value range from 0 to 2 24 -1 black to white. Red: bits 16-23 Green: bits 8-15 Blue: bits 0-7 Table 1.2.2b: Color constructors Here is a sample program that uses some of the methods in the Graphics class. import java.awt.; public class GraphicPanel extends Panel { GraphicPanel { setBackgroundColor.black; constant in Color class } public void paintGraphics g { g.setColornew Color0,255,0; green g.setFontnew FontHelvetica,Font.PLAIN,16; g.drawStringHello GUI World, 30, 100; g.setColornew Color1.0f,0,0; red g.fillRect30, 100, 150, 10; } public static void mainString args[] { Frame f = new FrameTesting Graphics Panel; GraphicPanel gp = new GraphicPanel; f.addgp; f.setSize600, 300; f.setVisibletrue; } } For a panel to become visible, it should be placed inside a visible window like a frame. Software Engineering 222 J.E.D.I More AWT Components Here is a list of AWT controls. Controls are components such as buttons or text fields that allow the user to interact with a GUI application. These are all subclasses of the Component class. Label Button Choice TextField Checkbox List TextArea CheckboxGroup Scrollbar Table 1.2.3: AWT Components The following program creates a frame with controls contained in it. import java.awt.; class FrameWControls extends Frame { public static void mainString args[] { FrameWControls fwc = new FrameWControls; fwc.setLayoutnew FlowLayout; more on this later fwc.setSize600, 600; fwc.addnew ButtonTest Me; fwc.addnew LabelLabe; fwc.addnew TextField; CheckboxGroup cbg = new CheckboxGroup; fwc.addnew Checkboxchk1, cbg, true; fwc.addnew Checkboxchk2, cbg, false; fwc.addnew Checkboxchk3, cbg, false; List list = new List3, false; list.addMTV; list.addV; fwc.addlist; Choice chooser = new Choice; chooser.addAvril; chooser.addMonica; chooser.addBritney; fwc.addchooser; fwc.addnew Scrollbar; fwc.setVisibletrue; } } Software Engineering 223 J.E.D.I

5.6.2 Layout Managers