Download presentation
Presentation is loading. Please wait.
Published byEvangeline Kennedy Modified over 9 years ago
1
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers Programming buttons Other GUI Components
2
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
3
3 Programming Actions: Implementing Listeners Can have multiple listeners listening for same event Source Component Event Listener 2 actionPerformed ( ActionEvent e ) ActionListener Listener 3 actionPerformed ( ActionEvent e ) ActionListener Listener 1 actionPerformed ( ActionEvent e ) ActionListener Event
4
4 Programming Actions: Implementing Listeners Listener acting on multiple events (of the same type) Source Components Event Listener actionPerformed ( ActionEvent e ) ActionListener Event
5
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
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
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
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!”); }
9
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()
10
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!”); }
11
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);...
12
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
13
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
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
15
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
16
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
17
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
18
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.
19
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
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
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
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); } );
23
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
24
24 Other Components: JTextField JSlider User defined - ColorBlock
25
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.
26
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.
27
27 ColorBlock (RGB) extends JLabel JLabel methods : –setBackground, setForeground ( Color ) –setText New methods: setColor, setRed,... ?
28
28 Other components: Search the API Reference Go through the Swing Tutorials Sample Code
29
29 Tutorial 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.