1 / 15

Graphical User Interfaces

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

jeslyn
Download Presentation

Graphical User Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Handling Appendix C Graphical User Interfaces CS 225

  2. 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

  3. CS 225 Class Hierarchy

  4. 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

  5. CS 225 Stand-alone Window Classes • Top-level container classes • JFrame, Frame • Dialog, JDialog • JWindow • We will use JFrame

  6. 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

  7. 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

  8. 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.

  9. 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

  10. 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.

  11. 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

  12. CS 225 Layout Managers • BorderLayout (see BorderLayoutDemo) • FlowLayout (see FlowLayoutDemo) • BoxLayout (see BuildBoxLayout) • GridLayout (see GridLayoutDemo) • CardLayout (see CardDemo) • There are other layout managers.

  13. 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

  14. 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

  15. 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

More Related