java gui graphical user interface
DESCRIPTION
JAVA GUI Graphical user interface. Eng. Amr Nagy. Introduction. GUI presents a user-friendly mechanism for interacting with an application. - PowerPoint PPT PresentationTRANSCRIPT
JAVA GUI JAVA GUI Graphical user interface
Eng. Amr Nagy
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.
Simple GUI-Based Input/Output with JOptionPane
// 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
Example
Some basic GUI components
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.
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)
The Delegation Model Example
source: JButton
+addActionListener(ActionListener listener)
listener: ListenerClass
ActionListener
+actionPerformed(ActionEvent event)
Register by invoking source.addActionListener(listener);
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
Selected Event Handlers
Exercise
Questions
13