13
JAVA GUI JAVA GUI Graphical user interface Eng. Amr Nagy

JAVA GUI Graphical user interface

Embed Size (px)

DESCRIPTION

JAVA GUI Graphical user interface. Eng. Amr Nagy. Introduction. GUI presents a user-friendly mechanism for interacting with an application. - PowerPoint PPT Presentation

Citation preview

Page 1: JAVA GUI  Graphical user interface

JAVA GUI JAVA GUI Graphical user interface

Eng. Amr Nagy

Page 2: JAVA GUI  Graphical user interface

Introduction

• GUI presents a user-friendly mechanism for interacting with an application.

• GUI (pronounced “GOO-ee”) gives an application a distinctive “look” and “feel.” Providing different applications with consistent, intuitive user interface components allows users to be somewhat familiar with an application, so that they can learn it more quickly and use it more productively.

Page 3: JAVA GUI  Graphical user interface

Simple GUI-Based Input/Output with JOptionPane

Page 4: JAVA GUI  Graphical user interface

// obtain user input from JOptionPane input dialogsString firstNumber =JOptionPane.showInputDialog( "Enter first integer" );

// display result in a JOptionPane message dialogJOptionPane.showMessageDialog( null, "The sum is " + sum,"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE );

Simple GUI-Based Input/Output with JOptionPane

Page 5: JAVA GUI  Graphical user interface

Example

Page 6: JAVA GUI  Graphical user interface

Some basic GUI components

Page 7: JAVA GUI  Graphical user interface

Event-Driven Programming

• Code is executed upon activation of events.

• An event can be defined as a type of signal to the program that something has happened.

• The event is generated by external user actions such as mouse movements, mouse clicks, and keystrokes, or by the operating system, such as a timer.

Page 8: JAVA GUI  Graphical user interface

The Delegation Model

An event is triggered by user actions on the source object; the source object generates the event object and invokes the handler of the listener object to process the event.

source: SourceClass

+addXListener(XListener listener)

listener: ListenerClass User Action

Trigger an event

XListener +handler(XEvent event)

Internal function of the source object

event: XEvent listener1 listener2 … listenern

+handler(XEvent

Register by invoking source.addXListener(listener);

Keep it a list

Invoke listener1.handler(event) listener2.handler(event) … listenern.handler(event)

Page 9: JAVA GUI  Graphical user interface

The Delegation Model Example

source: JButton

+addActionListener(ActionListener listener)

listener: ListenerClass

ActionListener

+actionPerformed(ActionEvent event)

Register by invoking source.addActionListener(listener);

Page 10: JAVA GUI  Graphical user interface

Selected User ActionsUser Action Source Object Event Type Generated

Click a button JButton ActionEvent

Press return on a text field

JTextField ActionEvent

Select a new item JComboBox ItemEvent, ActionEvent

Select item(s) JList ListSelectionEvent

Click a check box JCheckBox ItemEvent, ActionEvent

Click a radio button JRadioButton ItemEvent, ActionEvent

Select a menu item JMenuItem ActionEvent

Move the scroll bar JScrollBar AdjustmentEvent

Window opened, closed, etc.

Window WindoEvent

Component added or removed from the

container

Container ContainerEvent

Component moved, resized, etc.

Component ComponentEvent

Component gained or lost focus

Component FocusEvent

Key released or pressed Component KeyEvent

Mouse pressed, released, clicked, etc.

Component MouseEvent

Mouse moved or dragged Component MouseEvent

Page 11: JAVA GUI  Graphical user interface

Selected Event Handlers

Page 12: JAVA GUI  Graphical user interface

Exercise

Page 13: JAVA GUI  Graphical user interface

Questions

13