160 likes | 316 Views
Event Handling Appendix C. Graphical User Interfaces. 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
E N D
Event Handling Appendix C Graphical User Interfaces CS 225
CS 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
CS 225 Class Hierarchy
CS 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
CS 225 Stand-alone Window Classes • Top-level container classes • JFrame, Frame • Dialog, JDialog • JWindow • We will use JFrame
CS 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
CS 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
CS 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.
CS 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
CS 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.
CS 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
CS 225 Layout Managers • BorderLayout (see BorderLayoutDemo) • FlowLayout (see FlowLayoutDemo) • BoxLayout (see BuildBoxLayout) • GridLayout (see GridLayoutDemo) • CardLayout (see CardDemo) • There are other layout managers.
CS 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
CS 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/components.html
CS 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