In traditional applications, the user provided keyboard or file input, and the application acted on that, and then waited for more input. In GUI applications, the application can receive a variety of inputs:

mammetlizardlickΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

85 εμφανίσεις


CSS 442
, Carol Zander

In traditional
applications, the user provided keyboard
or file
input, and the application acted
on that, and then waited for more input.

In GUI applications, the application can receive a variety of inputs:




ouse input to the application (e.g., drawing)


mouse window actions (resize, minimize, etc)


repaint the screen due to other system activity

These inputs are collectively sent to the application as events

(event driven programming).

In Windows 95
and later, and in Java, the application specifies methods to be called when
certain categories of events happen. This is done with a class inheritance model. The system
provides a class with default methods for events in a category,
and the programmer
bclasses the system class and provides the methods to override.

(Note: Since this subclassing often involves just providing class methods, it's often convenient to use
Java interfaces rather than subclassing.

However, subclassing is usually simpler when

you can
use it because you can leave out methods you are not overriding.)

Frameworks, a set of classes and interfaces, are a form of design reuse.

The programmer
customizes as desired. Java's framework for building

s with a GUI are

* Abstract Window Toolkit

javax.* Java extended, particularly swing

Java history

Java 1.0
, 1.1

AWT (Abstract Window Toolkit)
; 1.1 had new event handling model

Java 1.2

(also known as Java2)

Swing, enhanced UI built on top of AWT

va 1.3, 1.4:

Bug fixes, etc., but no major changes

Java 1.5 (also known as version 5):

No major changes to Swing

(although language adds generics, enumerated types)

Use Swing whenever you can (not AWT). Most Swing components start with a J.

haracteristics of frameworks:


, application is shaped by the framework. A library of

components can often be used directly without modification.


implify the development
, can customize generic components.

For example, in Java, ne
w components are created by extending


class such

as JFrame or JPanel. Your new class inherits


extensive capabilities

of its superclass while adding new


and overriding to create a distinctive behavior.


hite box mechanism
, cus
tomization via inheritance, open to


black box mechanism
, customization via composition, new components.

Usually applications are a combination.

Java's GUI framework uses three main building blocks:


Components (e.g.

buttons, dial
og boxes, menus, text fields,
panels) Components appear in
containers, which are themselves a component.


Layout managers (arrange the components belonging to a container)


Event handling (e.g. push button, click mouse)

AWT has basic elements for GUI
s. It only uses features common to all systems. Thus it is simple,
but limited. Swing components are generally more powerful, do not depend on the native GUI system.

Component hierarchy (the GUI):


............ top
level container: wher
e other


components live

an OS window)




............ intermediate container: to simplify


positioning of labels, buttons

| |


other intermediate: scroll, tabbed panes

| | |

JButton JLabel etc

.... basic (or atomic) components

Every GUI component de
scends from the Component class (ancestor to JComponent).

"Atomic" components are labels, butto
ns, check boxes, etc. Some essential components are
Containers, e.g., JPanel which contain other subcomponents.

To get started, you need a top level application window:

JFrame theWindow = new JFrame("Optional Window title, my application");

Your top l
evel swing container will have a "content pane" that manages the components in the

A common container within the top container is a JPanel which provides a place for labels,
buttons, etc. To add a component o a top
level container, get its cont
ent pane and add:

JFrame myFrame = new JFrame();

JPanel myPanel = new JPanel();


Layout Management (size and position of components):


default for every content pane, 5 areas hold components


puts components in a single row or column


implement an area containing different components

at different times


default for every JPanel, lays components left to right


makes a bunch
of components equal in size and displays

in rows or columns


most sophisticated and flexible, places in a grid of

cells; rows and columns can have different heights/widths

When appropriate use “absolute positioning
” (no layout manager).

This simple example creates a JFrame with a JPanel in the center and label at the bottom:

JFrame theFrame =

JFrame("Simple Example");
// default BorderLayout

JPanel aPanel =


JLabel aLabel =

o World");

Container c = theFrame.getContentPane();

c.add(aPanel, BorderLayout.CENTER);
// in place of add above

c.add(aLabel, BorderLayout.SOUTH);

Events are generated by GUI components in response to user actions (an event source). Wh
en an
event occurs, an "event listener" is notified. An event source notifies an even listener of an event by
calling one of the listener's event handlers (methods for responding to events). The event handler is
passed an event object. Every event objec
t includes a getSource method which reveals the event
generating component.

For example, a button push generates an action event. A mouse click generates a mouse event.

Find a visual index to swing components at:

Summary of Listener API:


Swing GUI paints itself and repaints whenever necessary. When you

invoke a routine that alters the

it should automatically

repaint itself, but if it doesn't, it's a bug. The workaround is

invoke repaint on the component. If its size or position needs

to change, but does not automatically,
invoke revalidate before repaint.

For more complex graphic
s, extend some appropriate class and override the inherited method
paintComponent to draw the contents. For example,

// repaint this drawing whenever requested by the system

public void paintComponent(Graphics g) {



g.drawOval(40, 30, 100, 100);


g.fillRect(80, 50, 80, 80);


The paintComponent method is called by the system whenever it needs the window repainted
(triggered by some event), but if your code does something
that requires repainting, then call the
repaint method yourself. Always override paintComponent, but always call super.paintComponent() to
do the actual painting. Paint the whole thing from scratch. You won't create a Graphics object, but
will only use the

one given to you as a parameter of paintComponent.