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 εμφανίσεις

GUI

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:


--

keyboard


--

m
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
su
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

program
s with a GUI are


java.awt.
* 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


Ja
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.


C
haracteristics of frameworks:

--

R
euse
, application is shaped by the framework. A library of


useful
components can often be used directly without modification.


--

S
implify the development
, can customize generic components.


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

a


class such

as JFrame or JPanel. Your new class inherits

the


extensive capabilities

of its superclass while adding new

methods


and overriding to create a distinctive behavior.


--

W
hite box mechanism
, cus
tomization via inheritance, open to

examination;



black box mechanism
, customization via composition, new components.


Usually applications are a combination.


Java's GUI framework uses three main building blocks:

1.

Components (e.g.
,

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

2.

Layout managers (arrange the components belonging to a container)

3.

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):


JFrame



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

swing











components live

(
like
an OS window)


JComponent


|


JPanel



............ 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
container.

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();


myFrame.getContentPane().add(myPanel);



Layout Management (size and position of components):


BorderLayout
--

default for every content pane, 5 areas hold components


BoxLayout
--

puts components in a single row or column


CardLayout
--

implement an area containing different components


at different times


FlowLayout
--

default for every JPanel, lays components left to right


GridLayout
--

makes a bunch
of components equal in size and displays


in rows or columns


GridBagLayout
--

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 =
new

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


JPanel aPanel =
new

JPanel();


JLabel aLabel =
new

JLabel("Hell
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:

http://java.sun.com/docs/books/tut
orial/uiswing/components/components.html



Summary of Listener API:

http://java.sun.com/docs/books/tutorial/uiswing/events/api.html


Painting:

Swing GUI paints itself and repaints whenever necessary. When you

invoke a routine that alters the
component,

it should automatically

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

to
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) {



super.paintComponent(g);



g.setColo
r(Color.blue);



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



g.setColor(Color.green);



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.