SWING GUI

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

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

77 εμφανίσεις

GUI

Chapter

12

SWING GUI

1

2


JFrame

Japplet

JDialog

JWindow


JPanel

JInternalFrame

JInternalPane

JLayeredPane

JRootPane


JButton

JMenu

JLabel

JCheckBox

JRadioButton

JTextField

JTextArea


Components

Top Level
Containers

GUI

Intermediate
Containers

Atomic

3

Events

GUI

4

WindowListener



windowActivated
(
WindowEvent
)


windowDeactivated
(
WindowEvent
)


windowClosing
(
WindowEvent
)


windowClosed
(
WindowEvent
)


windowIconified
(
WindowEvent
)


windowDeiconified
(
WindowEvent
)


windowOpened
(
WindowEvent
)


MouseMotionListener


mouseDragged
(
MouseEvent
)


mouseMoved
(
MouseEvent
)

MouseListener


mousePressed
(
MouseEvent
)


mouseReleased
(
MouseEvent
)


mouseClicked
(
MouseEvent
)

)

Events

Window

GUI

Mouse

5

KeyListener


keyPressed
(
KeyEvent
)


keyReleased
(
KeyEvent
)


keyType
(
KeyEvent
)

Events

GUI

Keyboard

Button

TextField

CheckBox

RadioButton

Focus Events

6

No multiple
inheritance

GUI

7

Interface

GUI

“Overloaded”
term.

Totality of
methods
exposed by an
object.


Java reserved
word denoting
a set of
methods which
an object is
contracted to
expose.

8

Abstract
methods

GUI

9

Example

GUI

10

Using an
Interface

GUI

11

import
java.awt.event.WindowEvent
;

import
java.awt.event.WindowListener
;

import
javax.swing.JFrame
;

import
javax.swing.JMenuBar
;


public class
topFrame

extends
JFrame

implements
WindowListener

{



public
topFrame
() {


this("No title","200");


}


public
topFrame
(String title) {


this(title,"200");


}


public
topFrame
(String title, String size) {


super();


int

dimension = (new Double(size)).
intValue
();


this.setSize
(
dimension,dimension
);


this.setVisible
(true);


this.setTitle
(title);


addWindowListener
(this);
or

this.addWindowListener
(this);


}


public void
windowClosing
(
WindowEvent

e) {


System.out.println
("Window closing");


System.exit
(0);


}


public void
windowClosed
(
WindowEvent

e) {



….
implementation of the
WindowListener

Interface

}

Use interface because we need to inherit the
functionality of a frame. The functionality of
the listener is simpler

Frame

GUI

Import
required API

Delegate to self

Use full
constructor to
implement
partial
constructors

Ensure closing
the window
ends the
programme

12

import
java.awt.event.WindowAdapter
;

import
java.awt.event.WindowEvent
;


public class Listen extends
WindowAdapter

{



public void
windowActivated
(
WindowEvent

e) {


System.out.println
("WINDOW ADAPTER OUTPUT
-



WINDOW ACTIVATED");


}

}

Delegation to a
WindowAdapter

subclass.

All methods defined, only need to provide
overriden

methods.

Ease of implementation:


only specific methods need to be over
-
ridden


Normal to do it outside the listened object

topFrame

tF

= new
topFrame
();

Listen
wL

= new Listen();

tF.addWindowListner
(
wL
);


Can pass listener to component constructor

Listen
wL

= new Listen();

topFrame

tF

= new
topFrame
(
wL
);


Adapter Class

GUI

Adapter class

inheritance

13

More than one listener is possible


topFrame

tF

= new
topFrame
();

Listen
wL

= new Listen();

tF.addWindowListener
(
wL
);

tF.addWindowListener
(
tF
);


For instance open a connection to the network;
“grey out” some part of the GUI; store button
pushes for an undo functionality.


Different tasks require different objects to
implement them. Objects should do one job
not many.



The task is crucial the trigger to the task
is not


The listener is associated with the task not the
component.



Multiple
Listeners

GUI

14


Some actions do not extend outside the GUI


Changing the font, ……


Use an inner class


not visible from elsewhere.
Coupling is low and all the classes are
implemented in the same file.


class Listen extends
WindowAdapter

{



public void
windowActivated
(
WindowEvent

e) {


System.out.println
("WINDOW ADAPTER OUTPUT
-



WINDOW ACTIVATED");


}

}


Adapter


only overridden methods need to be
explicitly implemented.


Not public or private


not visible from elsewhere.


Inner Classes

GUI

15


Define the methods at the point when you add
the class.


Access the class is only possible in that one
context.


Component and the response to component is in
the same place.


Only have to look at one place for the component
and the action of the component.

Compact, high coherence, low coupling.


If the action extends out of the position, need to
introduce alternative communication methods

Anonymous
Classes

GUI

16


addWindowListener
(new
WindowAdapter
() {


public void
windowOpened
(
WindowEvent

e) {


System.out.println
("Window opened");


}


public void
windowClosing
(
WindowEvent

e) {


System.out.println
("Window closing");


System.exit
(0);


}


public void
windowClosed
(
WindowEvent

e) {


System.out.println
("Window Closed");


}


public void
windowActivated
(
WindowEvent

e) {


System.out.println
("Window activated");


}

});


Remember the constructor returns a pointer and
the
addWindowListener

stores the pointer in a
data structure.

When the appropriate action is performed a
message is sent to the object pointed to in the
data structure


there is no need to name it.


Anonymous
Class Syntax

GUI

17


Java portable


no idea what the underlying OS is


should run on many screens


Need to arrange
components.


Object available to do all the work


BorderLayout


BoxLayout


CardLayout


FlowLayout


GridLayout


GridBagLayout


SpringLayout


ViewportLayout


FlowLayout

just dumps things in the window.

Easy to use

Layout

GUI

OO solution

Chosen

18

Layouts

GUI

Border

Grid

Flow

Starts here

Moves across and
then down

Flow

hard to
control but
flexible.

19


Other possible top layer containers.


JApplet

clearly important.


Makes creating a web applet (almost) trivial.


Testing applets










Don’t forget
appletviewer
.

Lightweight, fast and allows reload/stop/restart
of applet under user control



Top Level
Containers

GUI

20


Interactions with a GUI are normally based on a
number of components


JButton
,
JMenu
,
JLabel
,
JCheckBox
,
JRadioButton
,
JTextField
,
JTextArea


Operation follows a common pattern.

Create the component.

Register the listener

Add to a container


topFrame

tf
;

tf

= new
topFrame
("Example Frame ","500");

tf.addWindowListener
(
tf
);

JButton

jB

= new
JButton
("Push me");

tf.add
(
jB
);


Button registers press


nothing happens

Button fills the whole container


Components

GUI

Container

Button

21


These are the methods for the
JPanel

class



JPanel

GUI

22


import
java.awt.event.ActionEvent
;

import
java.awt.event.ActionListener
;

import
java.awt.event.MouseAdapter
;

import
javax.swing.JButton
;


public class
testButton

extends
JButton

implements
ActionListener
{


private String label;

public
testButton
(String label) {


super(label);


this.label

= label;

// self delegation


this.addActionListener
(this);

}

public
testButton
() {


this("No label");

}

public void
actionPerformed
(
ActionEvent

e) {

System.out.println
("
\
"" + label + "
\
"" + " Button Pressed");

}



tf.setLayout
(new
FlowLayout
());

testButton

tB

= new
testButton
("Push me");

tf.add
(
tB
);

testButton

tB1 = new
testButton
("Push me as well ");

tf.add
(tB1);


Sizing,
Placing,
Activating
Components

GUI

Compoment

self delegation

Different
objects handle
interrupt

23

JFrame

contains components


each component has its own listener.



Nicely modular



One component cannot influence another




if one component sets up one part of an
application how does it communicate to
another part?


JFrame

contains components


common listener.



Not
modular



One component can influence another



communication easy




Problem

GUI

No
correct

answer, both
methods
supported.

24


















How do we arrange it?

Review

GUI

Top Level
Frame

Buttons,
sliders,
checkboxes,

Display Area

Listener

Listener

Listener

25


Create a GUI class to do the drawing



a listener class to respond to all the clicks.


Division of labour


GUI separate from the action code.



Action code has to determine what has been
pressed/slid/checked.


Two people can share


any more is hard



Concrete example:



Flow of traffic round a city





Design

GUI

26

Problem Decomposition


Road geometry: Lengths, junctions.


Road control: lights, speed limits


Car performance: (includes trucks, buses).


Size, acceleration, breaking, turning circle.


Road conditions: wet, dark,….


Driver psychology:


Driving aids: GPS, real time information feeds.


Traffic profile: number of cars, traffic mix


Car movement:


Traffic monitoring






Traffic+

GUI

27


Separate the GUIs into functional units














Each with its own listener
-

GUI and listener a
single responsibility.


The listeners need to communicate

Implement

GUI

Geometry

Control

Performance

Conditions

Aids

Movement

Geometry

Control

Performance

Conditions

Aids

Movement

28


Create top level GUI


Create geometry
gui


Create geometry
gui

listener


Register listener with
gui




repeat


Instantiate top level GUI




Press button
.


Start button calls listener



listener creates model


Create road geometry

Pseudo code

GUI

Start

Roads