No Slide Title
Download
Report
Transcript No Slide Title
CSC111H
Graphical User Interfaces (GUIs)
•
•
•
•
•
•
•
•
Introduction
GUIs in Java
Understanding Events
A Simple Application
The Containment Hierarchy
Layout Managers
Programming buttons
Other GUI Components
1
Programming Actions:
Implementing Listeners
• In Java, Listeners “listen” for events from
components.
1. Create a listener (an object)
2. Attach listener to source component
3. When event occurs, source lets listener know
ActionListener
actionPerformed ( ActionEvent e )
Source
Component
Event
Listener
2
Programming Actions:
Implementing Listeners
• Can have multiple listeners listening for same event
ActionListener
actionPerformed ( ActionEvent e )
Event
Listener 1
ActionListener
Event
Source
Component
actionPerformed ( ActionEvent e )
Listener 2
Event
ActionListener
actionPerformed ( ActionEvent e )
Listener 3
3
Programming Actions:
Implementing Listeners
• Listener acting on multiple events (of the same type)
Event
Event
ActionListener
actionPerformed ( ActionEvent e )
Event
Source
Components
Listener
4
Implementing Listeners
• What does a listener class look like?
import java.awt.*;
import java.awt.event.*;
// Simple listener class.
class AL implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “Something happened!”);
}
}
5
Implementing Listeners
• What does a listener class look like?
import java.awt.*;
import java.awt.event.*;
// Simple listener class.
class AL implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “Something happened!”);
}
}
6
Implementing Listeners
• What does a listener class look like?
import java.awt.*;
import java.awt.event.*;
// Simple listener class.
class AL implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “Something happened!”);
}
}
7
Implementing Listeners
• What does a listener class look like?
import java.awt.*;
import java.awt.event.*;
// Simple listener class.
class AL implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “Something happened!”);
}
}
8
Implementing Listeners
• What does a listener class look like?
import java.awt.*;
import java.awt.event.*;
// Simple listener class.
class AL implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println( “Something happened!”);
}
}
e.getActionCommand()
9
Implementing Listeners
• What does a listener class look like?
import java.awt.*;
import java.awt.event.*;
// Simple listener class.
class AL implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
System.out.println(“Something happened!”);
}
}
10
Implementing Listeners
• How do you add a Listener to a component?
...
AL listener = new AL();
// create 1 listener
JButton b1 = new JButton( “Button 1” );
JButton b2 = new JButton( “Button 2”);
// Add listener to buttons
b1.addActionListener(listener);
b2.addActionListener(listener);
...
11
Implementing Listeners
• What happens when I press a button?
• The Java system looks for the ActionListener object linked
to the button and calls the actionPerformed method.
ActionEvent
AL
actionPerformed ( ActionEvent e )
ActionEvent
listener
“Something happened!”
12
Implementing Listeners: Shorthand
• Don’t have to create a new class to implement listener.
• Can use an anonymous inner class:
JButton b = new JButton(“Button 1”);
b.addActionListener( new ActionListener() {
public void actionPerformed (ActionEvent event){
System.out.println(“Something happened!”);
} // end of method
} // end of anon. inner class
); // end of addActionListener method
13
Implementing Listeners: Shorthand
• Don’t have to create a new class to implement listener.
• Can use an anonymous inner class:
JButton b = new JButton(“Button 1”);
b.addActionListener( new ActionListener() {
public void actionPerformed (ActionEvent event){
System.out.println(“Something happened!”);
} // end of method
} // end of anon. inner class
); // end of addActionListener method
14
Implementing Listeners: Shorthand
• Don’t have to create a new class to implement listener.
• Can use an anonymous inner class:
JButton b = new Jbutton(“Button 1”);
b.addActionListener( new ActionListener() {
public void actionPerformed (ActionEvent event){
System.out.println(“Something happened!”);
} // end of method
Note:
} // end of anon. inner class
); // end of addActionListener method
Look for
“Sample$1.class” files
15
Listener Types:
Act that results in the event type
Listener
•
User clicks a button, presses Return while
typing in a text field, or chooses a menu
item.
• ActionListener
•
User moves a slider.
• ChangeListener
•
User closes a frame (main window).
• WindowListener
•
User presses a mouse button while the
cursor is over a component.
• MouseListener
•
User moves the mouse over a component.
• MouseMotionListener
16
Adapters: A Shortcut
• Listeners are interfaces
• To implement listeners, must define every
method of the interface
• Adapters are classes that implement listeners
with empty methods
• Simply extend adapter and override required
method instead of implementing every method of
listener
17
E.g. Shutting Down a Window:
• WindowListener declares many methods that
must be implemented (for opening, closing,
activating, deactivating, iconifing, deiconifing).
• WindowAdapter implements WindowListener
with empty methods. To use, inherit and overwrite
those of interest.
18
E.g. Shutting Down a Window:
• JFrame.EXIT_ON_CLOSE only available for Java
1.3 and up. Another way:
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
19
E.g. Shutting Down a Window:
• JFrame.EXIT_ON_CLOSE only available for Java
1.3 and up. Another way:
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
20
E.g. Shutting Down a Window:
• JFrame.EXIT_ON_CLOSE only available for Java
1.3 and up. Another way:
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
21
E.g. Shutting Down a Window:
• JFrame.EXIT_ON_CLOSE only available for Java
1.3 and up. Another way:
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
22
When to Use an Adapter
• If listener only has one method, no adapter is
needed or defined.
- e.g. ActionListener
• If you are going to implement every method, use
either the listener or the adapter
• If you only need a few of the methods, use an
adapter.
- e.g. WindowAdapter
23
Other Components:
• JTextField
• JSlider
• User defined - ColorBlock
24
Input of Character Strings
• The JTextField class implements a small area into which
the user can type a sequence of characters that the program can
then extract and process in some way.
• The constructor for this class takes two arguments: the initial
text string to appear in the text field, and an integer specifying
the width.
• The listener interface to implement for JTextField is the
ActionListener.
• The getText() method returns the string inside the field.
25
Sliders
• The JSlider class is used to let the user enter a numeric
value bounded by a minimum and maximum value
(Horizontal or Vertical).
• The listener interface to implement for JSlider is the
ChangeListener.
• The method called when the user scrolls is called:
stateChanged.
• Use getValue method to return position of the slider.
• Use setValue method to change position of the slider.
26
ColorBlock (RGB)
• extends JLabel
• JLabel methods:
– setBackground, setForeground
(Color)
– setText
• New methods: setColor, setRed,... ?
27
Other components:
• Search the API Reference
• Go through the Swing Tutorials
• Sample Code
28
Tutorial 1
29