Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Graphical User Interfaces - Object-Oriented Programming and Data Structures - Lecture Sl, Lecture notes of Object Oriented Programming

Major points from this lecture are: Graphical User Interfaces, Interactive Programs, Gui Motivation, Environment, Gui Statics, Gui Dynamics, Dynamics, Components, Component Tree, Component Examples . Object-Oriented Programming and Data Structures course includes program structure and organization, object oriented programming, graphical user interfaces, algorithm analysis, recursion, data structures (lists, trees, stacks, queues, heaps, search trees, hash tables, graphs), simple graph algorithms

Typology: Lecture notes

2012/2013

Uploaded on 08/20/2013

yumni
yumni 🇮🇳

5

(2)

27 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
IntroductiontoGraphicalUser
Interfaces(GUIs)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Graphical User Interfaces - Object-Oriented Programming and Data Structures - Lecture Sl and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Introduction

to

Graphical

User

Interfaces

(GUI

s^ )

Announcements

•^

A

will

be

posted

shortly,

please

start

early

•^

Prelim

Thursday

October

Uris

Hall

G

We

do

NOT

have

any

scheduled

makeup

exam

People

with

conflicts

can

take

the

exam

early.

•^

The

NORMAL

scheduled

time

is

•^

If^

you

have

a

conflict,

take

it

from

•^

Out

of

town

conflicts:

you’ll

take

it

during

one

of

these

two

time

periods,

supervised

by

some

trustworthy

person,

who

can

receive

exam/send

it

back

GUI

Motivation

•^ Interacting

with

a

program

-^ Program

‐Driven

=^

Proactive

-^ Statements

execute

in

sequential,

predetermined

order

-^ Typically

use

keyboard

or

file

I/O,

but

program

determines

when

that

happens • Usually

single

‐threaded

-^ Event

‐Driven

=^

Reactive

-^ Program

waits

for

user

input

to

activate

certain

statements

-^ Typically

uses

a^

GUI

(Graphical

User

Interface) • Often

multi

‐threaded

•^

Design...Which

to

pick?

-^

Program

called

by

another

program?

-^

Program

used

at

command

line?

-^

Program

interacts

often

with

user?

-^

Program

used

in

window

environment?

•^

How

does

Java

do

GUIs?

Java

Support

for

Building

GUIs

•^ Java

Foundation

Classes

-^ Classes

for

building

GUIs

-^ Major

components

-^ awt

and

swing

-^ Pluggable

look

‐and

‐feel

support

-^ Accessibility

API

-^ Java

2D

API

-^ Drag

‐and

‐drop

Support

-^ Internationalization

•^

Our

main

focus:

Swing

-^

Building

blocks

of

GUIs

-^

Windows

components

-^

User

interactions

-^

Built

upon

the

AWT

(Abstract

Window

Toolkit)

-^

Java

event

model

-^

Why

Swing?

-^

Easier

to

understand

than

SWT

-^

Lonnie

used

SWT

in

A

but

you

don’t

actually

need

to

understand

the

code

he

wrote

Java

Foundation

Classes

Pluggable

Look

‐and

‐Feel

Support

Controls

look

‐and

‐feel

for

particular

windowing

environment

E.g.,

Java,

Windows,

Mac

Accessibility

API

Supports

assistive

technologies

such

as

screen

readers

and

Braille

Java

2D

Drawing

Includes

rectangles,

lines,

circles,

images,

Drag

‐and

‐drop

Support

for

drag

and

drop

between

Java

application

and

a^

native

application

Internationalization Support

for

other

languages

GUI

Statics

and

GUI

Dynamics

Statics:

what’s

drawn

on

the

screen •^ Components

-^ buttons,

labels,

lists,

sliders,

menus,

...

-^ Containers:

components

that

contain

other

components

-^ frames,

panels,

dialog

boxes,

...

-^ Layout

managers:

control

placement

and

sizing

of

components

Dynamics: user interactions •^

Events

-^

button

‐press,

mouse

‐click,

key

press,

...

-^

Listeners:

an

object

that

responds

to

an

event

-^

Helper

classes

-^

Graphics,

Color,

Font,

FontMetrics,

Dimension,

...

Creating

a

Window

in

Swing

import

javax.swing.;*

public

class

Basic

public

static

void

main(String[]

args)

//create

the

window

JFrame

f

new

JFrame("Basic

Test!");

//quit

Java

after

closing

the

window

f.setDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE

f.setSize(200,

//set

size

in

pixels

f.setVisible(true);

//show

the

window

Things

to

notice

•^

Code

style

is

similar

Both

are

really

“customizing”

a

prebuilt

framework

You

write

little

bits

and

pieces

of

software

that

runs

in

the

context

of

the

preexisting

structure

•^

SWT

oriented

towards

somewhat

finer

control

•^

Swing

aims

for

a

sturdy

design,

but

can

be

harder

to

customize.

A

More

Extensive

Example

^

import

javax.swing.;*

^

import

java.awt.;*

^

import

java.awt.event.;*

^

public

class

Intro

extends

JFrame

{

^

private

int

count

=

0;

^

private

JButton

myButton

=^

new

JButton("Push

Me!");

^

private

JLabel

label

=

new

JLabel("Count:

"

+^

count);

^

public

Intro()

{

^

setDefaultCloseOperation(

EXIT_ON_CLOSE

);

^

setLayout(new

FlowLayout(FlowLayout.

LEFT

));

//set

layout

manager

^

add(myButton);

//add

components

^

add(label);

^

myButton.addActionListener(new

ActionListener()

{

^

public

void

actionPerformed(ActionEvent

e)

{

^

count++;

^

label.setText("Count:

"^

+^

count);

^

}

^

});

^

pack();

^

setVisible(true);

^

} ^

public

static

void

main(String[]

args)

{

^

try

{

^

UIManager.

setLookAndFeel

(UIManager.

getSystemLookAndFeelClassName

());

^

}^ catch

(Exception

exc)

{}

^

new

Intro();

^

} ^

}

GUI

Statics

•^

Determine

which

components

you

want

•^

Choose

a

top

‐level

container

in

which

to

put

the

components

JFrame

is

often

a

good

choice)

•^

Choose

a

layout

manager

to

determine

how

components

are

arranged

•^

Place

the

components

Component

Examples

-^

import javax.swing.;*

-^

import java.awt.;*

-^

public class ComponentExamples extends JFrame {

-^

public ComponentExamples() {

-^

setLayout(new FlowLayout(FlowLayout.

LEFT

));

-^

add(new JButton("Button"));

-^

add(new JLabel("Label"));

-^

add(new JComboBox(new String[] { "A", "B", "C" }));

-^

add(new JCheckBox("JCheckBox"));

-^

add(new JSlider(0, 100));

-^

add(new JColorChooser());

-^

setDefaultCloseOperation(

EXIT_ON_CLOSE

);

-^

pack();

-^

setVisible(true);

-^

}

-^

public static void main(String[] args) {

-^

try {

-^

UIManager.

setLookAndFeel

(UIManager.

getSystemLookAndFeelClassName

());

-^

} catch (Exception exc) {}

-^

new ComponentExamples();

-^

}

-^

}

More

Components

•^

JFileChooser

:^

allows

choosing

a

file

•^

JLabel

:^

a

simple

text

label

•^

JTextArea

:^

editable

text

•^

JTextField

:^

editable

text

(one

line)

•^

JScrollBar

:^

a

scrollbar

•^

JPopupMenu

:^

a

pop

‐up

menu

•^

JProgressBar

:^

a

progress

bar

•^

Lots

more!

Containers

A

container

is

a

component

that

Can

hold

other

components Has

a

layout

manager

Heavyweight

vs.

lightweight^ 

A^

heavyweight

component

interacts

directly

with

the

host

system

JWindow,

JFrame,

and

JDialog

are

heavyweight

Except

for

these

top

‐level

containers,

Swing

components

are

almost

all

lightweight^ 

JPanel

is

lightweight

^ There are three basic

top-level

containers  JWindow

: top-level window with no

border  JFrame

: top-level window with border

and (optional) menu bar  JDialog

: used for dialog windows

^ Another important container ^ JPanel

: used mostly to organize

objects within other containers

A

Component

Tree

JFrameJPanel

JPanel

JPanel

JPanel

JPanel

JPanel

JPanel

JComboBox (mi)

JComboBox (km)

JTextField (2000)

JSlider

JTextField (3226)

JSlider

JPanel

JPanel