Graphical User Interfaces Event Handling Appendix C Graphical

  • Slides: 15
Download presentation
Graphical User Interfaces Event Handling Appendix C

Graphical User Interfaces Event Handling Appendix C

Graphical Applications • Graphical application consists of a high-level window that contains components which

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

Class Hierarchy

Class Hierarchy

Components • Objects that are displayed on the screen • User interacts with components

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

Stand-alone Window Classes • Top-level container classes – JFrame, Frame – Dialog, JDialog –

Stand-alone Window Classes • Top-level container classes – JFrame, Frame – Dialog, JDialog – JWindow • We will use JFrame

Event Handling • Most components can generate events • java. awt. Event is the

Event Handling • Most components can generate events • java. awt. Event is the superclass for all events – – Action. Event Item. Event Mouse. Event … • Each component that generates an event needs to know what listener to send the event to

Listener Interfaces • Each event class has a corresponding Listener interface – – Action.

Listener Interfaces • Each event class has a corresponding Listener interface – – Action. Listener Item. Listener Mouse. Listener … • 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

Action. Listener public interface Action. Listener extends Event. Listener { public void action. Performed(

Action. Listener public interface Action. Listener extends Event. Listener { public void action. Performed( Action. Event e); } • Any class that implements this interface needs to define the action. Performed method which has code to execute the desired response to the event.

Listener classes • Examples from text create inner classes that implement the appropriate listener

Listener classes • Examples from text create inner classes that implement the appropriate listener interface. – see PDButton. UI. java • Another approach is to have the container class that holds the components implement the listener interface. – see GUIApp. java

JFrame • Create a GUI application by writing a class that extends Jframe •

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.

JPanel • JPanel is a light-weight container that can be used to organize a

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

Layout Managers • • • Border. Layout (see Border. Layout. Demo) Flow. Layout (see

Layout Managers • • • Border. Layout (see Border. Layout. Demo) Flow. Layout (see Flow. Layout. Demo) Box. Layout (see Build. Box. Layout) Grid. Layout (see Grid. Layout. Demo) Card. Layout (see Card. Demo) • There are other layout managers.

Combining Layout Managers • For a complex application, the layout managers may not do

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

Data Entry Components • JCheck. Box (see Check. Box. Demo) • JRadio. Button (see

Data Entry Components • JCheck. Box (see Check. Box. Demo) • JRadio. Button (see Border. Layout. Demo) – To work together, JRadio. Buttons need to be added to a Button. Group • • JCombo. Box (see Combo. Box. Demo) JText. Field (see Text. Field. Demo) JText. Area (see GUIApp) JLabel (see Text. Field. Demo) • … http: //java. sun. com/docs/books/tutorial/ui/features/co mponents. html

Building Menus • JMenu. Bar – A JFrame has a JMenu. Bar associated with

Building Menus • JMenu. Bar – A JFrame has a JMenu. Bar associated with it • JMenu – Add JMenu objects to the JMenu. Bar • JMenu. Item – Add JMenu. Items or JMenus to a Jmenu • See Draw. App. java