import java.awt.event.

Download Report

Transcript import java.awt.event.

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
Component Hierarchy: Exercise
ColorBlock (extends JLabel)
JSlider
JLabel
JTextField
JButton
2
Component Hierarchy: One Solution
JFrame (with its own content pane)
JPanel
JPanel
JPanel (Red)
JLabel JTextField JSlider
JButton (Reset)
ColorBlock
JPanel (Blue)
JPanel (Green)
JLabel JTextField JSlider
JLabel JTextField JSlider
3
Component Hierarchy: Another
Solution
4
Layout Management
• Layout =
Component Hierarchy + Layout Managers
• Layout managers are used to control the size and position of
components in containers.
• Specify how components are distributed within their container
• Manage components when frame is resized.
• The Java platform provides 5 commonly used layout managers:
– BorderLayout, FlowLayout, BoxLayout, GridLayout and
GridBagLayout.
5
Layout Management
BorderLayout
6
Layout Management
FlowLayout
7
Layout Management
BoxLayout
8
Layout Management
GridLayout
9
Layout Management
GridBagLayout
10
Layout Management
• Content panes use the BorderLayout by default.
• JPaneluses the FlowLayout by default.
11
Border Layout
North
West
Center
East
South
• Using a border layout:
JPanel panel = new JPanel();
JButton button = new JButton();
panel.setLayout( new BorderLayout() );
panel.add( button, BorderLayout.NORTH );
• Not all the positions in a border layout need to be filled.
12
What are the Layouts again?
13
Back to the ColourPicker
14
Adding borders to Components
• Some layout managers automatically put space between
components; others don’t.
• Empty Borders allow us to put spaces between components.
• The best candidates for empty borders are panels and labels.
• To set an empty border:
panel.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
15
Adding borders to Components
JLabel l1 = new JLabel("Label 1");
JLabel l2 = new JLabel("Label 2");
JLabel l3 = new JLabel("Label 3");
• But, if we add…
l1.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
l2.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
l3.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
16
Now what?
• We’ve tried to make the components look good.
• What about making the code neater, more compact
and re-usable?
17
class SampleFrame extends JFrame
{
JLabel l = new JLabel(“Label 1”);
JButton b = new JButton(“Button 1”);
JPanel p = new JPanel();
A better
way...
public SampleFrame(String title) {
super( title );
// or setTitle( title );
p.add( l );
p.add( b );
getContentPane().add( p );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
pack();
}
public static void main( String[] args ) {
SampleFrame f = new SampleFrame(“A Better Way”);
f.setVisible(true);
}
}
18
A better way...
• Can re-use SampleFrame class
• Components are data members of class
• Components set-up in constructor
• To call methods of JFrame (eg.getContentPane), no
longer need an object name - methods are inherited from
JFrame.
19
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
20
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
21
Programming Actions:
Implementing Listeners
• Listener acting on multiple events (of the same type)
Event
Event
ActionListener
actionPerformed ( ActionEvent e )
Event
Source
Components
Listener
22
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!”);
}
}
23
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!”);
}
}
24
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!”);
}
}
25
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!”);
}
}
26
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()
27
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!”);
}
}
28
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);
...
29
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!”
30
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
31
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
32
Implementing Listeners: Shorthand
• Don’t have to create a new class to implement listener.
• Can use an anonymous inner class:
JButton button = new Jbutton(“Button 1”);
button.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
33