Download presentation
Presentation is loading. Please wait.
1
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing Toolkit (AWT)
2
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 2 Today’s Lecture AWT Fundamentals –http://developer.java.sun.com/developer/onlineTraining/
3
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 3 History AWT in JDK 1.0 AWT JDK 1.1 JFC/Swing
4
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 4 Purpose of AWT Build Graphical User Interfaces Keep native platform look Uses native platform API
5
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 5 Hierarchy of GUI Components –superclass to all classes is Component –Component Button Canvas Checkbox Choice Label TextField Container –Panel –SCrollPan –Window –Frame List ScrollBar
6
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 6 Containers containers group widgets or other containers Containers can embed other containers containers can have a particular layout examples: –Panel –Applet –Frame
7
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 7 Widgets Button Canvas Label Checkbox Choice List TextField TextArea examples: –Button b = new Button(); –Label l = new Label(“OK”);
8
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 8 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);
9
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 9 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
10
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 10 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 }
11
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 11 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);
12
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 12 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(); }
13
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 13 Create HTML
14
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 14 View the file Use the appletviewer or use your browser to bring up the html file
15
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 15 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();
16
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 16 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
17
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 17 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);
18
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 18 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));
19
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 19 Types of layouts example –GridLayout –FlowLayout –CardLayout –BorderLayout –GridBagLayout
20
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 20 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
21
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 21 Nested Class Class EnclosingClass{ class 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 EnclosingClass InnerClass
22
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 22 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
23
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 23 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...} }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.