Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing Toolkit (AWT)

Download Report

Transcript Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing Toolkit (AWT)

Lecture 9
Object Oriented Programming in Java
Advanced Topics
Abstract Windowing Toolkit (AWT)
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
1
Today’s Lecture
• AWT Fundamentals
– http://developer.java.sun.com/developer/onlineTraining/
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
2
History
• AWT in JDK 1.0
• AWT JDK 1.1
• JFC/Swing
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
3
Purpose of AWT
• Build Graphical User Interfaces
• Keep native platform look
• Uses native platform API
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
4
Hierarchy of GUI Components
– superclass to all classes is Component
– Component
•
•
•
•
•
•
•
Button
Canvas
Checkbox
Choice
Label
TextField
Container
–
–
–
–
Panel
SCrollPan
Window
Frame
• List
• ScrollBar
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
5
Containers
•
•
•
•
containers group widgets or other containers
Containers can embed other containers
containers can have a particular layout
examples:
– Panel
– Applet
– Frame
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
6
Widgets
•
•
•
•
•
•
•
•
•
Button
Canvas
Label
Checkbox
Choice
List
TextField
TextArea
examples:
– Button b = new Button();
– Label l = new Label(“OK”);
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
7
Event Handlers
• Event handlers are registered with
Components and handle events that are
generated
– mouse clicks
– keyboard keys
– windows events
• Event handlers often implement ActionListener
• example:
– ActionListener theListener = new MyListener();
– Button b = new Button();
– b.addActionListener(theListener);
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
8
How to build a GUI
•
•
•
•
•
Add components to a Container
Create Event handler classes
Setup event handlers to respond to clicks,...
Display the GUI
Therefore you always need:
– one or more containers
– one or more Components
– one or more Event Handler
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
9
Extend the Applet class
import java.awt.Button;
import java.applet.Applet;
public class TheApplet extends Applet {
public void init() {
// add() one or more widgets to the applet itself
// create an event handler for a widget
}
}
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
10
Add a widget to the Applet
• Example:
//Create the widget
Button aButton = new Button(“some text”);
// add(Component c) is a method of Container
add(aButton);
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
11
Create an ActionListener
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
public class Beeper implements ActionListener {
public void actionPerformed(ActionEvent event) {
// get the conponent which initiated the event
Component c = (Component)event.getSource();
// beep
c.getToolkit().beep();
}
}
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
12
Create HTML
<html>
<applet code=AButton.class width=100 height=100>
</applet>
</html>
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
13
View the file
• Use the appletviewer
• or use your browser to bring up the html file
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
14
Standalone GUI
•
•
•
•
•
•
Create a class with a main method
Create a Frame: f = new Frame(“frame name”);
Create a Button and add it to the frame
Create an ActionListener class (reuse Beeper)
Register the event handlers with the button
Bring up the frame: f.pack(); f.show();
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
15
Enhanced standalone GUI
• Use Button class’ setActionCommand(String s)
method
• This method gives a name to the click “action”
of the button
• example:
– b.setActionCommand(“QUIT”)
• Add a few more buttons to your frame and
give them various actions
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
16
Enhanced Action Listener
• Within the actionPerformed(ActionEvent event) method,
check for which action command is performed
• 1) Use the ActionEvent method:
getActionCommand()
• 2) check to see which event was raised
• 3) when event “QUIT” perform a System.exit(0);
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
17
Layouts
• Layouts define how the components are laid
within the containers
• You have used a default layout all along
• You can assign a layout explicitly
• example
– Frame f = new frame(“frame name”);
– f.setLayout(new GridLayout(3,2));
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
18
Types of layouts
• example
–
–
–
–
–
GridLayout
FlowLayout
CardLayout
BorderLayout
GridBagLayout
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
19
Nested Classes
• Nested Classes are a powerful feature of Java
starting with JDK 1.1
• with nested classes, Java lets you define a
class as a member of another class. Such a
class is a nested class
• Nested class can see all of the class members
of its enclosing class, even the private ones
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
20
Nested Class
Class EnclosingClass{
class InnerClass{
...
}
}
EnclosingClass
InnerClass
An inner class is a nested class whose instance
exists within an instance of its enclosing class
and has direct access to the instance members
of its enclosing instance
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
21
Use Inner Classes for Listeners
• Create a class that extends Frame
• Add Components to the frame in the
constructor
• Define an inner class which extends
ActionListener
• Instantiate the inner class within the frame’s
constructor: the inner class now has access to
the Frame’s variables
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
22
Example of Inner Class
import java.awt.*;
import java.awt.event.*;
public class AppFrame extends Frame {
Button b1, b2;
public AppFrame(String title) {
super(title);
// instantiate buttons
…
// instantiate inner class
ActionListener al = new MyHandler();
b1.addActionListener(al);
}
class MyHandler extends ActionListener {
public void actionPerformed(ActionEvent event) {
// get the conponent which initiated the event
Component c = (Component)event.getSource();
if (c==b1) { //do this…}
if (c==b2) { // do that...}
}
}
June 1, 2000
Object Oriented Programming in Java (95-707)
Advanced Topics
23