1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple...
-
Upload
evangeline-kennedy -
Category
Documents
-
view
224 -
download
0
Transcript of 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple...
![Page 1: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/1.jpg)
1
CSC111HGraphical User Interfaces (GUIs)
• Introduction • GUIs in Java • Understanding Events • A Simple Application • The Containment Hierarchy • Layout Managers • Programming buttons• Other GUI Components
![Page 2: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/2.jpg)
2
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
Source Component
Event
Listener
actionPerformed ( ActionEvent e )
ActionListener
![Page 3: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/3.jpg)
3
Programming Actions: Implementing Listeners
• Can have multiple listeners listening for same event
Source Component
EventListener 2
actionPerformed ( ActionEvent e )
ActionListener
Listener 3
actionPerformed ( ActionEvent e )
ActionListener
Listener 1
actionPerformed ( ActionEvent e )
ActionListener
Event
Event
![Page 4: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/4.jpg)
4
Programming Actions: Implementing Listeners
• Listener acting on multiple events (of the same type)
Source Components
Event
Listener
actionPerformed ( ActionEvent e )
ActionListener
Event
Event
![Page 5: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/5.jpg)
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!”);
}
}
![Page 6: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/6.jpg)
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!”);
}
}
![Page 7: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/7.jpg)
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!”);
}
}
![Page 8: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/8.jpg)
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!”);
}
}
![Page 9: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/9.jpg)
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!”);
}
} e.getActionCommand()
![Page 10: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/10.jpg)
10
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!”);
}
}
![Page 11: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/11.jpg)
11
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);...
![Page 12: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/12.jpg)
12
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.
listener
actionPerformed ( ActionEvent e )
AL
ActionEvent
“Something happened!”
ActionEvent
![Page 13: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/13.jpg)
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
![Page 14: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/14.jpg)
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
} // end of anon. inner class
); // end of addActionListener method
![Page 15: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/15.jpg)
15
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
Note:
Look for “Sample$1.class” files
Note:
Look for “Sample$1.class” files
![Page 16: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/16.jpg)
16
Listener Types:
Act that results in the event type
• User clicks a button, presses Return while typing in a text field, or chooses a menu item.
• User moves a slider.
• User closes a frame (main window).
• User presses a mouse button while the cursor is over a component.
• User moves the mouse over a component.
Listener
• ActionListener
• ChangeListener
• WindowListener
• MouseListener
• MouseMotionListener
![Page 17: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/17.jpg)
17
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
![Page 18: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/18.jpg)
18
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.
![Page 19: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/19.jpg)
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);
}
}
);
![Page 20: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/20.jpg)
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);
}
}
);
![Page 21: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/21.jpg)
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);
}
}
);
![Page 22: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/22.jpg)
22
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);
}
}
);
![Page 23: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/23.jpg)
23
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
![Page 24: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/24.jpg)
24
Other Components:
• JTextField
• JSlider
• User defined - ColorBlock
![Page 25: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/25.jpg)
25
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.
![Page 26: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/26.jpg)
26
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.
![Page 27: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/27.jpg)
27
ColorBlock (RGB)
• extends JLabel
• JLabel methods:– setBackground, setForeground
(Color)– setText
• New methods: setColor, setRed,... ?
![Page 28: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/28.jpg)
28
Other components:
• Search the API Reference
• Go through the Swing Tutorials
• Sample Code
![Page 29: 1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.](https://reader034.fdocuments.in/reader034/viewer/2022051401/56649e915503460f94b968bf/html5/thumbnails/29.jpg)
29
Tutorial 1