JAVA GUI Programming

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

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

77 εμφανίσεις

1

CONTENTS

1.
Types of GUI Programs

2.
Swing components

3.
Layout

4.
Events and Listeners

5.
Convert Applet into Form Application.

2

Types of GUI Programs


There are two basic types of GUI program in Java:
Stand
-
alone
A
pplications
and applets.


An
applet is a program that runs in a rectangular area
on a Web page
.


A
Stand
-
alone
application is a program that runs on its
own, without depending on a Web browser

3

SWING COMPONENTS


GUI program uses objects (components) to make
interaction possible with users.


To use components in java programs, import
awt

(Abstract
W
indows Toolkits) or

swing
component

libraries. Swing is preferable over AWT as more
lightweight and is newer.


Import
java.awt
.*;

Or


Import
javax.swing
.*;


4

Swing components

Top
-
level Container


JFrame

(Form
Application)


JApplet

(Applet)


JDialog

Intermediate
-
level
Container


JPanel


JTabbedPane

Atomic components


JLabel
,
JTextField
,
JTextArea
,
JButton
,
JComboBox
,
JCheckBox



5

Simple frame application

import
javax.swing
.*;

public class
FrameApp

extends
JFrame

{

FrameApp
()
{

super("My Frame");

setDefaultCloseOperation
(
JFrame.EXIT_ON_CLOSE
);

setVisible
(
true
);

}

public
static void main(String[]
args
)
{


new
FrameApp
();


}

import
javax.swing
.*;

public class
FrameApp


{

public
static void main(String[]
args
)
{


Jframe

w = new
JFrame
();

w.

setDefaultCloseOperation
(
JFrame.EXIT_ON_CLOSE
);

w.setVisible
(
true);

}


6

Content Pane


A
basic
JFrame

has
a blank
content pane
;


One
can
either:


Add
things to that pane or


R
eplace
the basic content pane
entirely with
another container.


JPanel

is
one of fundamental
classes in Swing.
The basic
JPanel

is just
a blank rectangle.
There are two ways to make a useful
JPanel
:

1.
The
first is to
add other components

to the
panel;

2.
T
he
second is to
draw something

on the
panel.


7

Jpanel

as a container


Another way of using a
JPanel

is as a container to
hold other components. Java has many classes
that define GUI components. Before these
components can appear on the screen, they must
be added to a container.


JLabel

label = new
JLabel
("
Nhap

so
:");

JTextField

txtNhap

= new
JTextField
(15);

JButton

btnNhap

= new
JButton
("
Nhap
");

JPanel

content = new
JPanel
();

8

Jpanel

as a container

JLabel

label = new
JLabel
("
Nhap

so
:");

JTextField

txtNhap

= new
JTextField
(15);

JButton

btnNhap

= new
JButton
("
Nhap
");


JPanel

content = new
JPanel
();


content.add
(label);

content.add
(
txtNhap
);

content.add
(
btnNhap
);


setContentPane
(content);

9

Layout


The way components
are added to a
container,


In Java the
technique for laying out
components is to use a layout manager.


A
layout manager is an object
to
arrange
the components in a container;

10

Setting up GUI


Create a container and assign a layout manager
to it,


Create
components and add them to the
container,


Use
the container as the content pane of a
window or applet.

11

Setting up GUI

public class
demoTwo

{



public
static class
displayPanel

extends
JPanel

{


public
void
paintComponent
(Graphics g){

g.setColor
(
Color.RED
);

g.drawOval
(50, 50, 150, 150
);

}



}


public
static void main(String[]
args
) {

JFrame

w
=
new
JFrame
("Demo two");

w.setDefaultCloseOperation
(
JFrame.EXIT_ON_CLOSE
);

w.setSize
(400
, 300);

w.setVisible
(
true
);

displayPanel

aPanel

=
new
displayPanel
();

w.setContentPane
(
aPanel
);


}

}

12

Events and Listeners


GUIs
are largely event
-
driven;


T
he
program waits for events that are generated by the
user's
actions


When
an event occurs, the program responds by
executing an event
-
handling method.


In
order to program the behavior of a GUI,
an
event
-
handling methods
must be implemented to
respond to
the
events.

13

Events and Listeners


A
listener

is
an object that includes one or
more event
-
handling methods.


The
listener, has the responsibility of
responding to the event.


The
event itself is actually represented by a
third object, which carries information about
the type of event, when it occurred, and so on.

14

import
javax.swing
.*;

import
java.awt.event
.*;

public class

FrameApp

extends
JFrame

implements
ActionListener

{

FrameApp
(String
title){


super(title);


JButton

bttn

= new
JButton
("Click Me!");


bttn.addActionListener
(this);


getContentPane
().add(
bttn
);


}


public void
actionPerformed
(
ActionEvent

evt
) {


String title = "Greetings";


String message = "Hello from the Swing User Interface Library.";


JOptionPane.showMessageDialog
(null, message, title
,
JOptionPane.INFORMATION_MESSAGE
);


}

public
static void main(String[]
arg
) {

FrameApp

frame = new
FrameApp
("
Frame Application");

frame.setSize
(150,70
);

frame.show
();

}

}

15

Event Handling

public void
actionPerformed
(
ActionEvent

evt
) {


if
(
evt.getActionCommand
().equals("
Nhap
"))



stat.setText
(
txtNhap.getText
());


else
if


(
evt.getActionCommand
().equals("
Thoat
"))



dispose
();

}


Setting up Drawing Panel on Frame

public class
demoTwo

{


public static class
displayPanel

extends
JPanel

{



public
displayPanel
()
{




}


}


public static void main(String[]
args
)
{



JFrame

win = new
JFrame
("Demo two");



win.setDefaultCloseOperation
(
JFrame.EXIT_ON_CLOSE
);



win.setSize
(400, 300);



win.setVisible
(true);




displayPanel

aPanel

= new
displayPanel
();



win.setContentPane
(
aPanel
);




}

}

MouseEvent

&
MouseListener

MouseListener

is used
as a listener for mouse events,
an object must implement this
MouseListener

interface.

The
MouseListener

interface specifies five different
instance methods:

public void
mousePressed
(
MouseEvent

evt
);

public
void
mouseReleased
(
MouseEvent

evt
);

public
void
mouseClicked
(
MouseEvent

evt
);

public
void
mouseEntered
(
MouseEvent

evt
);

public
void
mouseExited
(
MouseEvent

evt
);

18

MouseMotionListener

MouseMotionListener

is used
as a listener for mouse
motion events
, an object must implement this
MouseMotionListener

interface to handle motion event

The interface
specifies
two
different instance methods:

public void
mouseDragged
(
MouseEvent

evt
);

public
void
mouseMoved
(
MouseEvent

evt
);

19

Mouse coordinates


MouseEvent

evt

parameter hold mouse coordinates
information when mouse event occurred.


The
coordinates of the mouse cursor
can be retrieved
by
calling
evt.getX
() and
evt.getY
().

The
coordinates
are expressed in the
coordinate system

of the
component that generated the event, where the top
left corner of the component is (0,0
).

20

Modifiers keys


The user can hold down certain modifier keys while
using the mouse. The possible modifier keys
include: the Shift key, the Control key, the ALT key
.
You
might want to respond to a mouse event
differently when the user is holding down a modifier
key. The
boolean
-
valued instance methods
evt.isShiftDown
(),
evt.isControlDown
(),
evt.isAltDown
() and
evt.isMetaDown
() can
be called
to test whether the modifier keys are pressed.

21

MouseMotionListeners

& Dragging


The methods for responding to mouse motion events
are defined in an interface named
MouseMotionListener
. This interface specifies two
event
-
handling methods:

public void
mouseDragged
(
MouseEvent

evt
);

public
void
mouseMoved
(
MouseEvent

evt
);

22

MouseMotionListeners

& Dragging


To
respond to mouse motion events, you must create
an object that implements the
MouseMotionListener

interface,
then
register that object to listen for
events
by
calling a component's
addMouseMotionListener

method. The object will then listen for
mouseDragged

and
mouseMoved

events associated with that
component.

23