Sadegh Aliakbary Sharif University of Technology Fall 2011

eyelashesnectarineSoftware and s/w Development

Nov 3, 2013 (3 years and 7 months ago)

72 views


Sadegh

Aliakbary

Sharif University of Technology

Fall 2011

Agenda


Graphical User Interface (GUI)


GUI Components


Event Handlers


Inner Classes


Anonymous
Inner Classes

Fall 2011

Sharif University of Technology

2

Graphical User Interface
(GUI)


GUI

presents
a mechanism for
interacting
with
application.


a user
-
friendly
mechanism


What are other mechanisms?


Console Applications


File


Web Applications





GUI is pronounced
“GOO
-
ee


Fall 2011

Sharif University of Technology

3

GUI Components


GUIs are built from
GUI components


sometimes called
controls

or
widgets


widget: short for window
gadgets


GUI
component:


an
object with which the
user interacts


via
the
mouse or
the
keyboard


Example?


Button, Textbox, Menu, …



Fall 2011

Sharif University of Technology

4


Fall 2011

Sharif University of Technology

5

Swing


AWT
was the early way for GUI in java


Abstract Window Toolkit


Java.awt

package


An
AWT
component’s behavior may be
different
between platforms
.



Swing
is a newer
approach


In this presentation we review
Swing
GUI components


javax.swing

package

Fall 2011

Sharif University of Technology

6

JOptionPane


JOptionPane class has simple static methods


for input and output


Using Dialog boxes



JOptionPane.
showInputDialog


Returns a String



JOptionPane.
showMessageDialog


Shows a dialog box



Fall 2011

Sharif University of Technology

7

JOptionPane Sample

String name =

JOptionPane.
showInputDialog
(
"Enter your name:"
);



JOptionPane.
showMessageDialog
(
null
,



"
Salam "
+name +
"!"
);


Fall 2011

Sharif University of Technology

8

Modal Dialog


JOptionPane dialogs are called
modal
dialogs


While
the dialog is on the
screen…


the
user
cannot
interact with
the rest of the
application

Fall 2011

Sharif University of Technology

9

JFrame


We create a window containing Swing GUI
components


This window object is usually instance
of
JFrame

or a
subclass of
JFrame


JFrame

provides
the basic
attributes and
behaviors of a
window


a
title bar at the
top


minimize and
maximize
buttons


close button

Fall 2011

Sharif University of Technology

10

JFrame

Example

JFrame

frame =
new

JFrame
();

frame.setSize
(300, 150);

frame.setVisible
(
true
);

frame.setLayout
(
new

FlowLayout
());

frame.setDefaultCloseOperation
(


JFrame.
EXIT_ON_CLOSE
);

Fall 2011

Sharif University of Technology

11

Swing GUI Components


JButton


JLabel


JTextField


JComboBox


JRadioButton


JMenu




Fall 2011

Sharif University of Technology

12

JLabel

and
JButton


JLabel label =
new

JLabel(
"Salam"
);


label.setToolTipText
(
"Tooltip1"
);


frame.add
(label);



JButton

button =
new

JButton
(
"Click!"
);


button.setToolTipText
(
"
salam

:
-
)"
);


frame.add
(button);


Fall 2011

Sharif University of Technology

13

Other GUI Components

JTextField

textbox =
new

JTextField
(10);

frame.add
(textbox);


JComboBox

combo =
new

JComboBox
(

new

String[]{
"Red"
,
"Blue"
,
"Green"
});

frame.add
(combo);

Fall 2011

Sharif University of Technology

14

Layout Management


You
must attach each GUI component to a
container


such
as
a
JFrame
.


You
typically must decide
where

to position each GUI
component


known as specifying the
layout


Java
provides several
layout managers


They help
you position
components

Fall 2011

Sharif University of Technology

15

FlowLayout


In
FlowLayout

layout manager:


components are placed on a container


from left to right in the order in which they’re added



When no more components can fit on the current
line


continue on
the next
line



Other layouts:


GridLayout
,
BoxLayout
, …


Fall 2011

Sharif University of Technology

16

Layouts


Three
ways
to
arrange
components
in a
GUI


Layout managers


Simple and Fast


Absolute
positioning


provides the greatest level of control
GUI’s
appearance.


Visual
programming in an
IDE


They generate codes


They may use layout managers


Fall 2011

Sharif University of Technology

17

Absolute Positioning


Set Container’s layout to null


Specify the absolute position of each GUI component


with
respect to the upper
-
left corner of the Container


by using Component methods


setSize

and
setLocation

or
setBounds


absolute positioning can be
tedious


unless you have a support by an IDE


Fall 2011

Sharif University of Technology

18

IDE Support


Many IDEs provide GUI design tools


You
can specify a component’s exact
size and
location


in
a visual manner by using the
mouse


simplifies creating
GUIs


each IDE generates this code
differently


You should know the underlying codes



Eclipse Plugin

Fall 2011

Sharif University of Technology

19

Extending
JFrame


You can also extend
JFrame

to create new frames…

Fall 2011

Sharif University of Technology

20

public
class
MyFrame

extends
JFrame
{


JLabel

label;


JButton

button;


JTextField

textbox;


JComboBox

combo;


public
MyFrame
(){



super("Frame Title");



label = new
JLabel
("Salam");



label.setToolTipText
("Label Tooltip");



add(label);



button = new
JButton
("Click!");



button.setToolTipText
("
salam

:
-
)");



add(button);



textbox = new
JTextField
(10);



add(textbox);



combo = new
JComboBox
(



new
String[]{"Red", "Blue", "Green"});



add(combo);


}

}


Fall 2011

Sharif University of Technology

21

Event Handling


GUIs are
event
driven


User interaction => events


An Event drives
the program to perform a
task


Some events:


clicking a
button


typing in
a text
field


s
electing
an item from a
menu


c
losing
a window


moving
the
mouse

Fall 2011

Sharif University of Technology

22

Event Handling (2)


event handler
:


The
code that performs a task in response to an
event



event handling
:


The overall
process of responding to
events

Fall 2011

Sharif University of Technology

23


Fall 2011

Sharif University of Technology

24


Fall 2011

Sharif University of Technology

25

Event Handlers

button

=
new

JButton
(
"Click!"
);

ActionListener

al =


new

ClickListener
();

button
.addActionListener
(al);


public

class

ClickListener


implements

ActionListener

{


public

void

actionPerformed
(
ActionEvent

e)

{



JOptionPane.showMessageDialog
(
null
,


“Salam!!!”
);


}

}

Fall 2011

Sharif University of Technology

26

Better Event Handlers


I want to show a message
-
box


According to the value of a component


For example a text
-
field or a combo
-
box



How can I do that?


Inner Classes

Fall 2011

Sharif University of Technology

27

public class
MyFrame

extends
JFrame
{


public
class
ClickListener

implements
ActionListener

{


public
void
actionPerformed
(
ActionEvent

e) {


JOptionPane.showMessageDialog
(null
,
textbox.getText
());


}


}


JButton

button;


JTextField

textbox;


public
MyFrame
(){


button
= new
JButton
("Click!");


button.addActionListener
(new
ClickListener
());


add(button
);


textbox
= new
JTextField
(10);


add(textbox
);


}

Fall 2011

Sharif University of Technology

28

Class Types

class

FriendlyClass
{


}

public

class

OuterClass

{


private

int

value
;



public

class

Inner{



public

void

f(){








}


}

}

Fall 2011

Sharif University of Technology

30

Friendly Class

Public class

Inner Class

Inner Classes


Declared in another class


Instantiated using a reference object of outer class


Has access to this object


The inner class can be static


No reference object of outer class is needed


No access to outer class is provided


Fall 2011

Sharif University of Technology

31

public

class

OuterClass

{



private

int

value

= 2;



class

Inner{



public

void

innerMethod
(){




OuterClass.
this
.
value

= 5;



}


}



public

void

outerMethod
(){



Inner
inner

=
new

Inner();



inner.innerMethod
();


}


public

static

void

main(String[]
args
) {



OuterClass

outer =
new

OuterClass
();



System.
out
.println
(
outer.
value
);



outer.outerMethod
();



System.
out
.println
(
outer.
value
);


}

}



Fall 2011

Sharif University of Technology

32

OuterClass.this

is implicitly
saved in
inner

object

public

class

OuterClass

{




private

int

value

= 2;




class

Inner{



public

void

f(){




OuterClass.
this
.
value

= 5;



}


}



public

static

void

main(String[]
args
) {






OuterClass

outer =
new

OuterClass
();





OuterClass.Inner

inner =
outer.
new

Inner();




System.
out
.println
(
outer.
value
);



inner.f
();



System.
out
.println
(
outer.
value
);


}

}


Fall 2011

Sharif University of Technology

33

Why we need
outer reference?

class

OuterClass

{


static

class

Inner{



public

void

f(){




System.
out
.println
(
"f() invoked"
);



}


}

}


public

class

MainClass

{


public

static

void

main(String[]
args
) {




OuterClass.Inner

in

=
new

OuterClass.Inner
()
;



in.f
();



}

}

Fall 2011

Sharif University of Technology

34

Inner Class
Specifiers


static


final


Access
Specifiers


public


private


Friendly (no
specifier
)


protected


Fall 2011

Sharif University of Technology

35

Anonymous Inner Class


An inner class


With no name


Created once


Used once


No access to this class from any other place


Once created and used


Fall 2011

Sharif University of Technology

36

interface

Inner{
void

innerMethod
();}


public

class

OuterClass

{


private

int

value

= 2;


public

void

outerMethod
(){



Inner
inner

=
new

Inner() {




public

void

innerMethod
() {





OuterClass.
this
.
value

= 5;




}



};



inner.innerMethod
();

}


public

static

void

main(String[]
args
) {



OuterClass

outer =
new

OuterClass
();



System.
out
.println
(
outer.
value
);



outer.outerMethod
();



System.
out
.println
(
outer.
value
);


}

}


Fall 2011

Sharif University of Technology

37

Anonymous Inner Class


Usually implements an interface


Or extends another class


And Overrides some special method


Main Application : Event Handlers


Fall 2011

Sharif University of Technology

38

public class
MyFrame

extends
JFrame
{


JButton

button;


JTextField

textbox;


public
MyFrame
(){


button
= new
JButton
("Click!");


button.addActionListener
(new
ActionListener
() {


public
void
actionPerformed
(
ActionEvent

e) {


JOptionPane.showMessageDialog
(null
,
textbox.getText
());


}


});


add(button
);


textbox
= new
JTextField
(10);


add(textbox
);


}

}

Fall 2011

Sharif University of Technology

39

Further Reading


javaw


Java Web Applications


Web Interface


SWT


Java Look and Feels


Fall 2011

Sharif University of Technology

40


Fall 2011

Sharif University of Technology

41

Fall 2011

Sharif University of Technology

42