Download presentation
Presentation is loading. Please wait.
1
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C
2
Fall 2007CS 225 Graphical Applications Graphical application consists of a high-level window that contains components which the user can interact with The java.awt and javax.swing packages contain classes for building graphical applications –the swing classes are newer
3
Fall 2007CS 225 Class Hierarchy
4
Fall 2007CS 225 Components Objects that are displayed on the screen User interacts with components by clicking the mouse (or sometimes, typing) Components generate events –each component that generates an event needs to know where to send it Containers are a subclass of Component –Containers can hold other Components
5
Fall 2007CS 225 Stand-alone Window Classes Top-level container classes –JFrame, Frame –Dialog, JDialog –JWindow We will use JFrame
6
Fall 2007CS 225 Event Handling Most components can generate events java.awt.Event is the superclass for all events –ActionEvent –ItemEvent –MouseEvent –… Each component that generates an event needs to know what listener to send the event to
7
Fall 2007CS 225 Listener Interfaces Each event class has a corresponding Listener interface –ActionListener –ItemListener –MouseListener –… When you create a component, you need to tell it which listener should get the events it generates –the component classed have add___Listener methods for doing this
8
Fall 2007CS 225 ActionListener public interface ActionListener extends EventListener { public void actionPerformed( ActionEvent e); } Any class that implements this interface needs to define the actionPerformed method which has code to execute the desired response to the event.
9
Fall 2007CS 225 Listener classes Examples from text create inner classes that implement the appropriate listener interface. –see PDButtonUI.java Another approach is to have the container class that holds the components implement the listener interface. –see GUIApp.java
10
Fall 2007CS 225 JFrame Create a GUI application by writing a class that extends Jframe The constructor contains code to add all the desired components and register the appropriate listener for each component.
11
Fall 2007CS 225 JPanel JPanel is a light-weight container that can be used to organize a set of related components. As with JFrame, you can create your own panel class that extends JPanel. A JPanel can also be used as a drawing surface
12
Fall 2007CS 225 Layout Managers BorderLayout (see BorderLayoutDemo ) FlowLayout (see FlowLayoutDemo ) BoxLayout (see BuildBoxLayout ) GridLayout (see GridLayoutDemo ) CardLayout (see CardDemo ) There are other layout managers.
13
Fall 2007CS 225 Combining Layout Managers For a complex application, the layout managers may not do exactly what you want. You can add one or more JPanels with different layout managers to your main container to get more complex arrangements of components
14
Fall 2007CS 225 Data Entry Components JCheckBox (see CheckBoxDemo ) JRadioButton (see BorderLayoutDemo ) –To work together, JRadioButtons need to be added to a ButtonGroup JComboBox (see ComboBoxDemo ) JTextField (see TextFieldDemo ) JTextArea (see GUIApp ) JLabel (see TextFieldDemo ) … http://java.sun.com/docs/books/tutorial/ui/features/co mponents.html http://java.sun.com/docs/books/tutorial/ui/features/co mponents.html
15
Fall 2007CS 225 Building Menus JMenuBar –A JFrame has a JMenuBar associated with it JMenu –Add JMenu objects to the JMenuBar JMenuItem –Add JMenuItems or JMenus to a Jmenu See DrawApp.java
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.