cs 112 gui 06 may 2008 bilkent. java gui api containers: ◦ contain other gui components. e.g,...

32
CS 112 GUI CS 112 GUI 06 May 2008 Bilkent

Post on 21-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

CS 112 GUICS 112 GUI

06 May 2008Bilkent

Java GUI APIJava GUI API Containers:

◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog.

Components:◦ Buttons, ComboBox, List, Slider, Label etc.

Helpers:◦ Font, Color, Dimension, LayoutManager etc.

Frame with normal window

controlsOptional Menu

Three containers in Frame with

Border Layout

UI-components inside

containers each with own layout

Understanding the GUI

UI -containers have list of

UI -components

Each UI -component is a class with paint method & lists of

Event listeners

{Frame}

{label}

{textfield}

{button}

components

Setting up the GUI

Extend Frame class In constructor

• Create instances of containers& add them to Frame

• Create instances of components& add them to containers or Frame

Possibly override paint method

UI-components added to components list Painting Frame

1.paints Frame borders2.calls Frame paint method3.calls paint method of each object in component list

Hint: Employ layout managers

to arrange components in

containers

A simple example

import javax.swing.* ;

class FirstFrame extends J Frame { public FirstFrame() {

setTitle("FirstFrame");setSize(300, 200);

}}

public class FirstTest { public static void main(String[] args) {

J Frame frame = new FirstFrame();frame.show();

}}

Layout ManagerLayout Manager

Flow Layout◦ By default

Border LayoutGrid LayoutBox Layout

Why layout?

Layouts provide flexibility in different environments with different screen resolutions

For X axis:

GraphicsGraphicsPreviously, we have seen GUI

components, their relationships, containers, layout managers.

Now we will see how to paint graphics on GUI components

In this example if you resize the frame, line disappears, so how can we avoid this problem and display line permanently?-by inheriting Jlabel to draw line!

--The paintComponent method is invoked, whenever a component is first displayed or redisplayed!

--To draw things on component you first declare a class that extends a swing GUI and override paintComponent method

The tester of SmileyFace:

Drawing Strings, Lines, Drawing Strings, Lines, Rectangles, and OvalsRectangles, and Ovals…

Drawing arcsDrawing arcs

Draw PolygonsDraw Polygons

Event Driven Event Driven ProgrammingProgramming

- In ED programming, code is executed when an event occurs

- The program interacts with user and the events drive its execution.

- Event is defined as a signal to the program that something has happened.

- The component which an event is generated is called source object or source component. E.g., button

Events & Event Handling

Example… User clicks on a button Button is source of event object Event object passed to associated listener object Listener object executes associated method

to perform desired task (save file, quit program, …)

Eventlistenerobject

(executesmethod)

EventObjectEvent

cause(mouse,

keyboard,timer, …)

EventSourceobject

Setting up Event Handling

Create listener class Using new or existing class, simply Implement desired event listener interface Putting code for desired action in its methods

In application (e.g. Frame)

Create instance of listener class Add as listener of source object

• can have any number of listeners for each event• Source & listener can be same object!

Understanding Events

When button is pressed actionPerformed method

of every item in button’s actionListeners list called

Similarly for textfield When Frame close button

is pressed windowClosing method of

every item in Frame’s windowListeners list called

{Frame}

{label}

{textfield}

{button}

components

ActionListeners

ActionListenersactionPerformed

{MyListener}

actionPerformed

WindowListeners

windowClosing

Event Classes

AWTEventEventObject

AdjustmentEvent

ComponentEvent

TextEvent

ItemEvent

ActionEvent

InputEvent

WindowEvent

MouseEvent

KeyEvent

ContainerEvent

FocusEvent

PaintEvent

ListSelectionEvent

Event Examples

User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener

User closes a frame (main window) WindowListenerUser presses a mouse button while the cursor is over a

component MouseListenerUser moves the mouse over a component

MouseMotionListenerComponent becomes visible ComponentListenerComponent gets the keyboard focus FocusListenerTable or list selection changes ListSelectionListener

How to Implement an Event Handler

In the declaration for the event handler class, specify that the class either implements a listener interface or extends a class that implements a listener interface. For example:

public class MyClass implements ActionListener { Register an instance of the event handler class as a

listener upon one or more components. For example: someComponent.addActionListener(instanceOfMyClass)

Implement the methods in the listener interface. For example: public void actionPerformed(ActionEvent e) { .../ /code that reacts to the action... }

Simple exampleSimple example

Simple example with inner Simple example with inner classclass

Inner class is declared when it is only used by outer classAnd inner class can use the reference data and methods in outer class

Another inner class Another inner class exampleexample

Inner class also can be shortened by using anonymous inner classes(without a name),

Another exampleAnother example

Mouse ExampleMouse Example