Vous êtes sur la page 1sur 2

1.

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
? Containers (JPanel, JScrollPane)
? Hold other Components
? Top Level Windows (JFrame, JDialog)
? Are not contained in other Containers
? Interact with the native windowing system
Swing Event Model
? 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
? Implement the Listener interface in the
same class
? For small problems (The CurrencyConverter class)
public class MyClass extends Jframe implements
ActionListener {
?
component.addActionListener(this);
public void actionPerformed(ActionEvent ae) {
?
}
}
Anonymous Inner Classes
? A nameless class with a single instance
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
// Handle the ActionEvent
}
});
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

Vous aimerez peut-être aussi