Outline - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Outline

Description:

Presses ENTER in a text field ... Creating a Mouse Handler. Create a new component, add it to the container ... Register a mouse event handler on the component ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 28
Provided by: cs11
Category:
Tags: outline

less

Transcript and Presenter's Notes

Title: Outline


1
Outline
  • JFrames
  • Menus
  • Event handling

2
Recap
  • Basics of GUIs
  • Swing components
  • JComponent
  • Various widgets
  • Containers
  • LayoutManagers
  • Applets

3
JFrame Example
4
JFrames
  • A JFrame is a container which uses a new window
    on-screen.
  • Can have an associated menu, as shown in the
    example

5
JFrame Example code
  • import javax.swing..
  • import java.awt.
  • public class JFrameTest
  • public static void main(String args)
  • JFrame frame new JFrame(Test Frame)
  • frame.setSize(400, 400)
  • JMenuBar menuBar new JMenuBar()
  • JMenu fileMenu new JMenu(File)
  • JMenuItem aboutMenuItem new
    JMenuItem(About)
  • JMenuItem exitMenuItem new JMenuItem(Exit)
  • fileMenu.add(aboutMenuItem)
  • fileMenu.add(exitMenuItem)
  • menuBar.add(fileMenu)

6
JFrame Code, cont
  • JMenu editMenu new JMenu(Edit)
  • JMenuItem cutMenuItem new JMenuItem(Cut)
  • JMenuItem copyMenuItem new JMenuItem(Copy)
  • JMenuItem pasteMenuItem new
    JMenuItem(Paste)
  • editMenu.add(cutMenuItem)
  • editMenu.add(copyMenuItem)
  • editMenu.add(pasteMenuItem)
  • menuBar.add(editMenu)
  • frame.setJMenuBar(menuBar)
  • JButton button1 new JButton(Cut)
  • JButton button2 new JButton(Copy)
  • JButton button3 new JButton(Paste)

7
JFrame Code, cont
  • frame.getContentPane().setLayout(new
    FlowLayout())
  • frame.getContentPane().add(button1)
  • frame.getContentPane().add(button2)
  • frame.getContentPane().add(button3)
  • frame.setVisible(true)
  • Components must be added to the content pane of
    the frame (use getContentPane() to access it)
  • setVisible makes the frame (in)visible

8
JFrame Use
  • A typical application uses a number of different
    JFrames over the course of execution
  • Usually, all frames get created at start-up, and
    those not used initially are set invisible
  • As buttons are pressed and menu items selected,
    different frames are set invisible and visible

9
Events
  • Some sort of input (usually generated by the
    user, but possibly by network or another program)
  • E.g.,
  • pressing a key
  • clicking or moving the mouse
  • resizing or closing a window
  • etc

10
Event Listeners
  • The Swing components have built-in functionality
    to determine when particular events occur
  • Applications generally need to respond to certain
    events e.g., exit the program if the quit button
    is pressed
  • Register Event Listeners with components to get a
    notification when an event occurs

11
Event Listeners, Conceptually
Hey dude, I have been clicked on !
OK, I got you, Ill take it from here.
Component
Event Listener
12
Event Handling
  • Register event handler with component
  • Otherwise, component just ignores events, since
    it doesnt know anyone is interested
  • Write event handling code
  • What needs to be done on each event is
    application-specific, so you have to tell the
    computer what to do when a particular event
    occurs (by writing code to do it)
  • Note more than one event handler can be
    registered for a given component

13
ActionEvent/Listener
  • ActionEvent occurs when user
  • Clicks on a button
  • Presses ltENTERgt in a text field
  • Generally, when a full action (with respect to a
    certain widget) is performed
  • ActionListener is an interface that can be used
    to listen to ActionEvents.

14
Creating an ActionListener
  • Create the component and add it to some
    containerJButton button new JButton(Quit)co
    ntainer.add(button)
  • Register action event handler with the
    componentQuitEventHandler handler new
    QuitEventHandler()button.addActionListener(han
    dler)

15
Creating an ActionListener, cont
  • Write a handler for the action event
  • public class QuitEventHandler implements
    ActionListener
  • public void actionPerformed(ActionEvent e)
  • System.out.println(Quit button pressed! Bye
    bye)
  • System.exit(0)
  • In most cases, the parameter (the event object)
    can be ignored. It contains information such as
    event time, keyboard modifiers, etc.

16
Mouse Events
  • Mouse enter/exits a window
  • A mouse button is pressed or released
  • A mouse button is clicked (a press and release)
  • A mouse button is double-clicked
  • MouseListener contains a method to handle each of
    these events

17
Creating a Mouse Handler
  • Create a new component, add it to the
    containerJPanel panel new JPanel()container.a
    dd(panel)
  • Register a mouse event handler on the
    componentMouseEventHandler handler new
    MouseEventHandler()panel.addMouseListener(handle
    r)

18
Mouse Listener Example, cont
  • Write a mouse event handler
  • public class MouseEventHandler implements
    MouseListener
  • public void mouseClicked(MouseEvent e)
    System.out.println(Clicked!)
  • public void mousePressed(MouseEvent e)
    System.out.println(Pressed!)
  • public void mouseReleased(MouseEvent e)
    System.out.println(Released!)
  • public void mouseEntered(MouseEvent e)
    System.out.println(Entered!)
  • public void mouseExited(MouseEvent e)
    System.out.println(Exited!)

19
Window Listeners
  • Window is opened
  • Window is activated/de-activated (receives/loses
    focus)
  • Window is iconified/de-iconified
  • Window is closing

20
Key Listeners
  • Key is pressed
  • Key is released
  • Key is typed (press and release)
  • The key event includes which key was pressed

21
Summary of Important Events
  • ActionEvent
  • button click, text field enter, list double-click
  • MouseEvent
  • click, enter, exit, press, release
  • WindowEvent
  • open, close, de/iconify, de/activate
  • Key Event - press, release, type a key

22
Event Adapters
  • Often one only cares about one or two events in
    any particular listener interface
  • E.g., most applications only care about window
    closing (so they can properly clean up and exit)
    from WindowListener
  • Adapters implement corresponding listener
    interfaces with empty method bodies

23
Adapters, contd
  • Allows us to subclass the adapter, and only
    implement the methods we care about
  • Otherwise, wed need to write empty method bodies
    ourselves to fully implement the listener
    interfaces
  • Every listener has a corresponding adapter
  • e.g., MouseListener has MouseAdapter, KeyListener
    has KeyAdapter, etc.

24
Adapter Example
  • public class QuitKeyListener extends KeyAdapter
  • public void keyTyped(KeyEvent e)
  • if ( e.getKeyChar() q e.getKeyChar()
    Q )
  • System.exit(0)
  • // No need to have empty keyPressed or
    keyReleased definitions //inherited from
    KeyAdapter

25
Event Handling Example
  • SwingAction.java (available on the lecture
    notes page on the course website.)

26
Summary
  • Use JFrames to create your own windows and JMenus
    to piece menus together
  • Use Event Listeners to catch user input events,
    and cause your applications to react to the user
  • Use Adapters if only a couple of events from a
    given listener interface are desired

27
Next Class
  • Exception Handling
Write a Comment
User Comments (0)
About PowerShow.com