Abstract Windowing Toolkit 2
Abstract Windowing Toolkit 2
introduce Java graphical components (e.g. Textfields, Buttons, and Labels) show how Java programs can be made to react to user actions on Java graphical components show how Java applications can be run from inside web browsers (Java applets)
Frames
A frame is a window with a title bar and a border The Frame class is a subclass of Container class Container class objects may have other components (e.g. Buttons) added to them using the add method
A typical Java GUI will create and display one or more frames To make a frame visible the message setVisbible(true) must be sent to the frame
Layout Managers
Governs how components are arranged inside a Container object The Container method setLayout allows the programmer to specify which layout manager (e.g. FlowLayout) is desired for a particular container object The size of a Container object should be set explicitly by the programmer as well Can use pack() to set size to accommodate preferred sizes of components)
Frame Example
public class TwoButtons { Frame f; Button redButton, blueButton; public TwoButttons() { f = new Frame(Two Buttons Frame); redButton = new Button(Red); blueButton = new Button(Blue); f.setLayout(new Flowlayout()); f.add(redButton); f.add(blueButton); f.pack(); f.setVisible(true); } }
TextField
a box into which the user can type text text can be edited (backspace, delete, etc.) text can be retrieved using getText() text can be set using setText()
3.
This is usually done in a programmer defined class like ClosableFrame and all classes needing these services would extend ClosableFrame
Checkboxes
Used to allow user to select one or more items Always has a label Program often needs to react to the user selection(s) Declaration example:
Checkbox powerBrakes = new Checkbox(Power Brakes); Checkbox powerSteering = new Checkbox(Power Steering); Checkbox ac = new Checkbox(Air Conditioning); add(powerBrakes); add(powerSteering); add(ac);
Programming Checkboxes
Program is declared with implements ItemListener Checkbox is registered by calling addItemListener Event is handled using itemStateChanged argument type ItemEvent The ItemEvent argument is used to tell which item triggered the event by calling getSource
Radio Buttons
A group of Checkboxes in which only one item can be selected at a time Implemented using a Java CheckboxGroup Items are declared initially as selected (true) or unselected (false) Example:
CheckboxGroup gender; Checkbox maleCheck = new Checkbox(Male, gender, true); Checkbox femaleCheck = new Checkbox(Female, gender, true);
Drawing in a Frame
To draw in a Frame you need to the override the frames paint method:
public void paint(Graphics g)
Graphics objects are defined by the Java runtime system and are used for drawing operations The Frame should be considered to be a grid with upper left coordinates (0,0) and positive coordinates (x,y) for the lower right
GridLayout
Frame is declared as a grid of fixed size (e.g. two rows and three columns) Components are placed in the grid left to right and top to bottom
BorderLayout
Frame is divided into north, south, east, west, and center Components are placed by the programmer in the desired location using the add method
Scrollbars
The leftmost position of a horizontal scrollbar corresponds to some integer value and the rightmost position corresponds to a larger integer value Scrollbars can be displayed vertically User movement options
Clicking either end button causes bubble to move in unit increments Clicking the are between bubble and end button causes movement in 10 unit increments Clicking and dragging the bubble in the desired direction