SLIDE 1
Inf1BOb ject-Oriented Programming
OOP Lab Exercise 3 (week 5) – Widgets
Hutchins 7th-9th February, 2006
1 Introduction
In OOP assignment 2, you will learn how to implement a simple class hierarchy — a hierarchy of shapes. This lab introduces a similar concept, called a widget. So what is a widget? You see them every time you turn on a computer. Widgets are the building blocks that make up a graphical user interface, or GUI, which is the part of the application that you see on the screen and interact with. Buttons, check boxes, menus, scroll bars, sliders, text fields etc. are all widgets. As a general rule of thumb, if you can see it on the screen, or click on it with your mouse, it’s a widget. Widgets are interesting for two reasons. First of all, if you want to write a program with a GUI, then you have to learn how to use them. Second, the invention of object-
- riented programming itself was motivated in part by the need to deal with widgets.
Classes, inheritance, and class hierarchies have proved to be very useful mechanisms for organizing GUI libraries, and they are thus an excellent illustration of the benefits
- f object-oriented programming.
Unfortunately, a modern widget library such as that supplied by Java is immensely complicated, and difficult even for experienced programmers to master. We will not be teaching the Java libraries in any detail in this course. Instead, we will provide you with a much smaller, simpler library, which was specially developed for Informatics 1B.
1.1 Events
Like shapes, each widget has a size and position on the screen. Each kind of widget is also responsible for drawing itself in an appropriate way — e.g. buttons draw text surrounded by a box, while scroll bars draw a rectangle with arrows. Most importantly, widgets respond to events. Every time the user moves the mouse, clicks on something, or presses a key, it generates an event. The Java libraries will process that event, and then call a method
- f the appropriate widget. When you click on a button, the button object receives a