Escolar Documentos
Profissional Documentos
Cultura Documentos
00 Tutorial 6
Swing
Agenda
Introduction to Swing
Swing event handling
Anatomy of a Swing Application
Online Java Tutorial
http://java.sun.com/docs/books/tutorial/uiswing/
GUI Classes
Components (JLabel, JButton)
Present Graphical User Interface Elements
Interact with the user
Event Sources
Components generate events upon User/System interaction
Event Objects
Events are encapsulated by instances of subclasses of
EventObject (ActionEvent, MouseEvent, SelectionEvent)
Event Listeners
Listen and react to EventObjects
Event sources are delegated to Event listeners by the
programmer.
Many to Many relationship
Two Ways to Handle Events
component.addActionListener(this);
}
});
Model View Controller
View manages the graphical/textual
output
Controller interprets user interaction
and directs the Model/View to change
as necessary
Model manages behaviour of
application.
Responds to requests for state from View
Responds to requests for change of state from Controller
SwingApplication
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
Swing Border class & the BorderFactory
Anonymous inner class to handle the ActionEvent
Component Layout
FlowLayout
BorderLayout
getContentPane()
pack()
setVisible(true)
Excercise
Add a reset Button to our application
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.