Upload
metheans791455933
View
222
Download
0
Embed Size (px)
Citation preview
8/12/2019 Abstract Windowing Toolkit 2
1/23
introduce Java graphical components (e.g. Textfields,
Buttons, and Labels)
show how Java programs can be made to react touser actions on Java graphical components
show how Java applications can be run from inside
web browsers (Java applets)
Abstract Window Toolkit (AWT)
8/12/2019 Abstract Windowing Toolkit 2
2/23
8/12/2019 Abstract Windowing Toolkit 2
3/23
Frames
A frameis a window with a title bar and a border
The Frameclass is a subclass of Containerclass
Containerclass objects may have othercomponents (e.g. Buttons) added to them using theaddmethod
A typical Java GUI will create and display one or
more frames
To make a frame visible the messagesetVisbible(true)must be sent to the frame
8/12/2019 Abstract Windowing Toolkit 2
4/23
Layout Managers
Governs how components are arranged inside aContainerobject
The Containermethod setLayoutallows the
programmer to specify which layout manager(e.g. FlowLayout) is desired for a particularcontainer object
The size of a Containerobject should be set
explicitly by the programmer as well Can use pack()to set size to accommodate
preferred sizes of components)
8/12/2019 Abstract Windowing Toolkit 2
5/23
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);
}
}
8/12/2019 Abstract Windowing Toolkit 2
6/23
Frame Inheritance Example
public class TwoButtons extends Frame {
Button redButton, blueButton;
public TwoButttons() {
super(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);
}
}
8/12/2019 Abstract Windowing Toolkit 2
7/23
Other Simple Java Components
Label
contains some text that can be set using a
constructor or the method setLabel 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()
8/12/2019 Abstract Windowing Toolkit 2
8/23
Java AWT Event Model
Including reactive program components involves:
1. Having the class header declare itself asimplementing the ActionListenerinterface
2. Typically in the class constructor the class instance
registers itself as being interested in listening for
events from a newly created component
3. One method (e.g. actionPerformed) of the
ActiveListenerinterface is defined
8/12/2019 Abstract Windowing Toolkit 2
9/23
Frame Inheritance Example
public class classnameextends Frame implements ActionListener {
Button buttonname;
public classname() {
buttonname= new Button(button label);
add(buttonname);
buttonname.ActionListener(this);
}
public void actionPerformed(ActionEvent e) {
what to do when button is pushed
}
}
8/12/2019 Abstract Windowing Toolkit 2
10/23
Closing the Window
You must implement the WindowListenerand its seven required methodswindowActivated, windowClosed,
windowClosing, windowDeactivated,
windowIconified, windowDeiconified,
windowOpened
This is usually done in a programmer defined
class like ClosableFrameand all classesneeding these services would extendClosableFrame
8/12/2019 Abstract Windowing Toolkit 2
11/23
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);
8/12/2019 Abstract Windowing Toolkit 2
12/23
Programming Checkboxes
Program is declared with implementsItemListener
Checkbox is registered by callingaddItemListener
Event is handled using itemStateChangedargument type ItemEvent
The ItemEventargument is used to tellwhich item triggered the event by callinggetSource
8/12/2019 Abstract Windowing Toolkit 2
13/23
Radio Buttons
A group of Checkboxes in which only one item canbe selected at a time
Implemented using a Java CheckboxGroup
Items are declared initially as selected (true) orunselected (false)
Example:CheckboxGroup gender;
Checkbox maleCheck =new Checkbox(Male, gender, true);
Checkbox femaleCheck =
new Checkbox(Female, gender, true);
8/12/2019 Abstract Windowing Toolkit 2
14/23
Processing Radio Buttons
public void compute() {
boolean female =
(gender.getSelectedCheckbox() == femaleCheck);
if ((bodyMassIndex > 27.8) ||
(female && (bodyMassIndex > 27.3))
lHigh.setText(This is considered high);else
1High.setText(this is not considered high);
}
8/12/2019 Abstract Windowing Toolkit 2
15/23
Drawing in a Frame
To draw in a Frameyou need to the overridethe frames paintmethod:public void paint(Graphics g)
Graphics objects are defined by the Javaruntime system and are used for drawingoperations
The Frameshould be considered to be a gridwith upper left coordinates (0,0) and positivecoordinates (x,y) for the lower right
8/12/2019 Abstract Windowing Toolkit 2
16/23
Typical Drawing Codepublic class MyDrawing extends ClosableFrame {
public MyDrawing() {
}
public void paint(Graphics g) {
g.drawLine();
g.drawImage();
}
}
8/12/2019 Abstract Windowing Toolkit 2
17/23
Repaint and Update
The paintmethod requires an argument and
actionPerformeddoes not know what to
supply when called The method repaintwill clear the Frame
and then call paintalong with supplying the
needed missing argument
You can prevent clearing the Frame whenusing repaintby overriding the method
updatebefore calling repaint
8/12/2019 Abstract Windowing Toolkit 2
18/23
Panel and Canvas Classes
The Panelclass is container subclass that is
used to reserve a rectangular portion of a
Frameto place other components The Canvasclass is not a Container
subclass, but does allow you to reserve aportion of a Frameto draw in
8/12/2019 Abstract Windowing Toolkit 2
19/23
Comparing Layout Managers
FlowLayout
Default frame layout
Components are placed on new line only when needed
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 desiredlocation using the addmethod
8/12/2019 Abstract Windowing Toolkit 2
20/23
8/12/2019 Abstract Windowing Toolkit 2
21/23
Typical GUI Frame
Panel p1 = new Panel();
p1.setLayout(new GridLayout(2,1));
p1.add(component);
p1.add(component);
Panel p2 = new Panel();
p2.setLayout();
p2.add(component);
setLayout(new BorderLayout());
add(North, p1);
add(Center, p2);
8/12/2019 Abstract Windowing Toolkit 2
22/23
Scrollbars
The leftmost position of a horizontal scrollbarcorresponds to some integer value and therightmost position corresponds to a largerinteger 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 buttoncauses movement in 10 unit increments
Clicking and dragging the bubble in the desireddirection
8/12/2019 Abstract Windowing Toolkit 2
23/23
Transforming a Java Program
into an Applet1. Set the size you want the applet to be in the
HTML file
2. Add the import statement to the Java codejava.applet.*;
3. Change header from extends Frametoextends Applet
4. Change constructor heading to
public void start();
5. Eliminate any Java code that is meaninglessin applets