Java Programming Java Programming

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

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

55 εμφανίσεις

JavaProgramming
Java

Programming
GraphicalUserInterface
Graphical

User

Interface

(GUI)
ArashArashHabibiHabibiLashkariLashkari
Ph.D. Candidate of UTM Universit
y
Ph.D. Candidate of UTM Universit
y
Kuala Lumpur, Malaysia
Kuala Lumpur, Malaysia
All Rights Reserved ©
All Rights Reserved © 20102010, www.ahlashkari.com, www.ahlashkari.com
1
www.ahlashkari.com
Overvie
w

JavaclassestosupportprogramswithGraphical

JFrame

JRadioButton

Java

classes

to

support

programs

with

Graphical

User Interface (GUI):

JButton

JLabel

JList

JCombobox

JTextField

JPasswordField

JTextArea

JSlider

JCheckBox
JSlider

GUIcomponentsaredefinedin
javaxswing

GUI

components

are

defined

in

javax
.
swing
2
www.ahlashkari.com
J
Frame
J

AwindowwhereotherGUIcomponentswill

A

window

where

other

GUI

components

will

be displayed




3
www.ahlashkari.com
JFrame
C
()

C
onstructor : JFrame
()

Member method:
setTitle(String s)
setSize
(
int
w
int
h)
title of window
width of window
setSize
(
int
w
,
int
h)
height of window
x position of window
setLocation(intx, inty)
tDfltClOti
(
JFEXITONCLOS
y position of window
se
tD
e
f
au
ltCl
ose
O
pera
ti
on
(
JF
rame.
EXIT
_
ON
_
CLOS
E)
visible or not visible
setVisible(booleanv)
4
www.ahlashkari.com
J
Frame
J
im
p
ort
j
avax.swin
g
.*;
pjg
public class TestJFrame
{
public static void main (String[] args)
{
{
JFrame frame1 = new JFrame();
frame1.setTitle("Window");
fm
1
tSi(
200
150
);
f
ra
m
e
1
.se
tSi
ze
(
200
,
150
);
frame1.setLocation(200, 400);
frame
1
setDefaultCloseOperation(JFrameEXITONCLOSE);
frame
1
.
setDefaultCloseOperation(JFrame
.
EXIT
_
ON
_
CLOSE);
frame1.setVisible(true);
}
}
}
5
www.ahlashkari.com
J
Frame
J
6
www.ahlashkari.com
J
Button
J

Constructor:
JButton(Strings)

Constructor:

JButton(String

s)
Label on button

Example:
new JButton(“OK”);
7
www.ahlashkari.com
J
Labe
l
J

Constructor: JLabel()

Constructor: JLabel(String s)

Constructor:
JLabel
(Strings
int
j)
Text on label

Constructor:

JLabel
(String

s
,
int
j)
Text on label
alignment:
SwingConstants.LEFT
SwingConstantsCENTER
8
SwingConstants
.
CENTER
SwingConstants.RIGHT
www.ahlashkari.com
J
Labe
l
J

Membermethod:
setText(Strings)

Member

method

:

setText(String

s)

Example :
newJLabel(

Entername:

);
new

JLabel(Enter

name

:

);
or
JLabel lbl = new JLabel();
lbl.setText(“Enter name : ”);
9
www.ahlashkari.com
J
TextField
J

Constructor:
JTextField(intj)

Constructor:

JTextField(int

j)

Example :
newJTextField(
15
);
new

JTextField(
15
);

Constructor: JTextField(String s, int j)

Example :
new JTextField(“Name here”, 15);
10
www.ahlashkari.com
J
TextField
J
Member method:

setText
(Strings)

setText
(String

s)

to set the contents of text field to s.
getText
()

getText
()

Return the contents of text field.
tCl
(
it
j)
se
tC
o
l
umns
(
i
n
t
j)

To set the size of text field to j.
setEditable(booleaneditable)

To set whether the contents of text field is
ditblBdfltttfildiditbl
e
dit
a
bl
e.
B
y
d
e
f
au
lt
, a
t
ex
t

fi
e
ld

i
s e
dit
a
bl
e.
11
www.ahlashkari.com
J
PasswordField
J

constructor:
JPasswordField(intj)

constructor:

JPasswordField(int

j)

Column size of
Column size of
password field

Example :
new JPasswordField
(
10
)
;
(
)
12
www.ahlashkari.com
JCheckBox
Constructor: JCheckBox(String s)

Example:

Example

:
new JCheckBox(“Java”);
label on check box
Iiil f hk b
Constructor:
JCheckbox(Strings,booleanstatus)
I
n
i
t
i
a
l
status
f
or

c
h
ec
k b
ox
JCheckbox(String

s,

boolean

status)
Example :
JChkB(“J”t)
13
new
JCh
ec
kB
ox
(“J
ava

,
t
rue
)
;
www.ahlashkari.com
J
CheckBox
J

Membermethod:
isSelected()

Member

method:

isSelected()


return status of check box, which is true
(ifselected)or
false
(ifotherwise)
(if

selected)

or

false
(if

otherwise)
14
www.ahlashkari.com
J
RadioButton
J
Constructor: JRadioButton(String s)
El

E
xamp
l
e :
new JRadioButton(“Java”);
label on check box
Constructor:
JRdiBtt
(Sti
bl
tt)
Initial status for check box
JR
a
di
o
B
u
tt
on
(St
r
i
ng s,
b
oo
l
eans
t
a
t
us
)
Example :
15
new JRadioButton(“Java”,true);
www.ahlashkari.com
J
RadioButton
J

Membermethod:
isSelected()

Member

method:

isSelected()


return status of radio button, which is true
(ifselected)or
false
(ifotherwise)
(if

selected)

or

false
(if

otherwise)
16
www.ahlashkari.com
A
ddin
g
GUI Com
p
onents
gp
ToaddGUIcomponentsintoJFrame:

To

add

GUI

components

into

JFrame:
1.
Set the layout of GUI components

by specifying a particular Layout
Manager
-Layout Manager is defined in
j
ava.awt
2.
Add components
-using method add()
17
www.ahlashkari.com
A
ddin
g
GUI Com
p
onents
gp
public static void main (String[] args)
{
{
JFrame frame1 = new JFrame();
frame1.setTitle("Window");
frame
1
setSize(
200
150
);
frame
1
.
setSize(
200
,
150
);
frame1.setLocation(200, 400);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame
1
setVisible(true);
frame
1
.
setVisible(true);
frame1.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label = new JLabel("Enter name : ");
JTextField tf = new JTextField
(
"Name here"
,
15
)
;
(,
)
JButton bt = new JButton("OK");
frame1.add(label);
frame1.add(tf);
18
frame1.add(bt);
}
www.ahlashkari.com
A
ddin
g
GUI Com
p
onents
gp
19
www.ahlashkari.com
MoreGUIs
More

GUIs
www.ahlashkari.com
20
Back to
J
RadioButton..
J

Example:

Example

:
:
JRadioButtonbtn
1
btn
2
btn
3
;
JRadioButton

btn
1
,
btn
2
,
btn
3
;
btn1= newJRadioButton("Windows
", true
);
btn2 = new JRadioButton("Linux");
btn3 = new JRadioButton
(
"Solaris"
);
();
frame1.add(btn1);
frame1.add(btn2);
frame1.add(btn3);
:
21
www.ahlashkari.com
Back to
J
RadioButton..
J

Output:

Output:

when other buttons are selected…
22
www.ahlashkari.com
ButtonGrou
p
p

Object ButtonGroupneeds to be created and linked
torelatedradiobuttons
to

related

radio

buttons
.

Example:
:
JRadioButton btn1, btn2, btn3;
btn1= newJRadioButton("Windows", true);
btn2 = new JRadioButton("Linux");
btn
3
=
newJRadioButton(
"
Solaris
"
);
btn
3

new

JRadioButton(Solaris);
ButtonGroup gp = new ButtonGroup();
gp.add(btn1);
gp.add(btn2);
dd(bt
3
)
gp.a
dd(bt
n
3
)
;
frame1.add(btn1);
frame1.add(btn2);
frame
1
.add(btn
3
);
23
frame
1
.add(btn
3
);
:
www.ahlashkari.com
Usin
g
ButtonGrou
p

g
p

Output:

Output:

when other buttons are selected…
24
www.ahlashkari.com
J
List
J

Constructor:
JList(Object[]list)

Constructor:
JList(Object[

]

list)
Array containing
Array containing
list of component
JList items

Member method:
setVisibleRowCount(int c)

set number of rows on list to be displayed without using
scrollbar
25
scroll

bar
www.ahlashkari.com
J
List
J

Example:

Example

:
:
String[]item
=
{
"
Line
""
Rectangle
""
Circle
"
String[]

item

{Line
,
Rectangle
,
Circle
,
"Triangle", "Hexagon", "Square"};
JList lst = new JList
(
item
);
();
lst.setVisibleRowCount(4);
frame1.add(lst);
:
26
www.ahlashkari.com
J
List
J

Output:

Output:
Howmanyitemsarevisible?Why?

How

many

items

are

visible?

Why?
27
www.ahlashkari.com
J
ScrollPane
J

The object list should be linked to object
JSllP
bfiiif
JS
cro
llP
ane
b
e
f
ore putt
i
ng
i
t
i
nto
f
rame

Exam
p
le:
p
String[] item = {"Line", "Rectangle", "Circle",
"
Triangle
"
,
"
Hexagon
"
,
"
Square
"
};
Triangle,

Hexagon,

Square};
JList lst = new JList(item);
lst.setVisibleRowCount
(
4
);
(
);
JScrollPane jsp = new JScrollPane(lst);
frame1.add(jsp);
// frame1.add(lst);
28
www.ahlashkari.com
J
ScrollPane
J

Output:

Output

:

Onl
y
4 items visible at a time
,
use scroll
p
ane
y
,p
to see other items
29
www.ahlashkari.com
Back to
J
List…
J
Member method:
tSltiMd
se
tS
e
l
ec
ti
on
M
o
d
e
Set choice of mode
ListSelectionModelSINGLESELECTION
ListSelectionModel
.
SINGLE
_
SELECTION
ListSelectionModel.SINGLE_INTERVAL_SELECTION
ListSelectionModel.MULTIPLEINTERVALSELECTION
__
30
www.ahlashkari.com
J
ComboBox
J
Constructor:
JComboBox(Object[ ] list)

Create ob
j
ect
J
ComboBoxwhich re
p
resents a combo
j
p
box component whose item consists of objects in an
array called list.
El

E
xamp
l
e:
:
String[]item={
"
Line
""
Rectangle
""
Circle
"
String[]

item

=

{Line
,
Rectangle
,
Circle
,
"Triangle", "Hexagon", "Square"};
JComboBox cb= new JComboBox(item);
frame
1
.add(
cb
);
31
frame
1
.add(
cb
);
:
www.ahlashkari.com
J
ComboBox
J

Output:

Output:
32
www.ahlashkari.com
J
ComboBox
J

Afterclickingthedownarrow:

After

clicking

the

down

arrow:
33
www.ahlashkari.com
J
TextArea
J

constructor:

constructor:

JTextArea(int br, int lj)
Number of rows and
columns of text area
JTextArea(String tks, int br, int lj)
Iitil tt i
I
n
iti
a
l t
ex
t i
n

text area
34
www.ahlashkari.com
J
TextArea
J
Exam
p
le:
p
:
JTextArea ta = new JTextArea(7, 30);
JSllPjJSllP(t)
JS
cro
llP
ane
j
sp = new
JS
cro
llP
ane
(t
a
)
;
frame1.add(jsp);
:
Output:
35
www.ahlashkari.com
J
TextArea
J
Example:
:
:
JTextArea ta = new JTextArea("Since the inception of
computer, many programming languages have been
created."
,
7
,
30
);
,
,
);
JScrollPane jsp = new JScrollPane(ta);
frame1.add(jsp);
:
Output:
36
www.ahlashkari.com
J
Slide
r
J
Constructor:
JSlider()
JSlider()

Example:
newJSlider();
Create default object JSlider :
horizontal
n
0
100
new

JSlider();
ra
n
ge

0
..
100
Initial value 50.
JSlider(int direction)
JSlider.HORIZONTAL
Example:
JSlid(JSlidHORIZONTAL)
JSlider.VERTICAL
37
new
JSlid
er
(JSlid
er.
HORIZONTAL)
;
www.ahlashkari.com
J
Slide
r
J
JSlider(intdirintminintmaxintn)
JSlider(int

dir
,
int

min
,
int

max
,
int

n)


Create object JSliderwith range min..maxand its
initial value n.

Example:
new JSlider(JSlider.HORIZONTAL, -100, 100, 0);
38
www.ahlashkari.com
JSlider
Member method:
void
setMajorTickSpacing
(
int
)
void
setMajorTickSpacing
(
int
)

•Set the spacing of major tick marks
void
setMinorTickSpacing
(
int
)
void

setMinorTickSpacing
(
int
)

•Set the spacing of minor tick marks
void
setPaintTicks
(
boolean
)
void

setPaintTicks
(
boolean
)
•true –to display major and minor tick
marks
void setPaintLabels(boolean)
•true –to display label for major tick
marks
39
marks
www.ahlashkari.com
J
Slider

Example:

Example:
:
JSlidlidJSlid(JSlidHORIZONTAL
JSlid
er s
lid
er = new
JSlid
er
(JSlid
er.
HORIZONTAL
,
10, 90, 30);
slider.setMajorTickSpacing(40);
lidMiTikSi(
10
)
s
lid
er.set
Mi
nor
Ti
c
kS
pac
i
ng
(
10
)
;
slider.setPaintLabels(true);
slider.setPaintTicks(true);
frame1.add(slider);
:
40
www.ahlashkari.com
J
Slider

Output:

Output:
41
www.ahlashkari.com
HihfSiCt
Hi
erarc
h
y o
f

S
w
i
ng
C
omponen
t
s
Basic components of GUI Swing inherit class JComponent
Dimension
Font
Classes in
package java.awt
1
LayoutManager
Font
FontMetrics
Object
Color
Panel
Applet
JApplet
Component
Graphics
Container
Frame
Window
JFrame
*
Dialog
JComponent
JDialog
Swing components
inpackagejavaxswing
42
in

package

javax
.
swing
www.ahlashkari.com
JComponent
.
JCheckBoxMenuItem
JButton
JMenuItem
JCheckBoxMenuItem
AbstractButton
JMenu
JRadioButtonMenuItem
JComponent
JToggleButton
JCheckBox
JRadioButton
JEditorPane
JTextField
JTextComponent
JTextArea
JPasswordField
JComboBox
JList
JMenuBar
JOptionPane
JFileChooser
JScrollBar
JScrollPane
JTabbedPane
JPanel
JLabel
JInternalFrame
JLa
y
eredPane
JPopupMenu
JProgressBar
JPane
JSeparator
JSplitPane
JSlider
JTable
JTableHeade
r

JToolBar
JToolTi
p
JTree
JRootPane
JColorChooser
y
p
43
www.ahlashkari.com
Questions
44
www.ahlashkari.com
THANK YOU
ArashHabibiLashkari
PHD. Candidate of UTM
Kuala Lumpur Malaysia
Kuala Lumpur
,
Malaysia
Feb, 2010
THEEND
THE

END
www.ahlashkari.com
45