GUIs in the Java MiniProject - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

GUIs in the Java MiniProject

Description:

GUIs in the Java Mini-Project. Imperial Oil Summer Institute for Computer Studies Educators ... in the mini-project, the state is recorded in the queryResult ... – PowerPoint PPT presentation

Number of Views:180
Avg rating:3.0/5.0
Slides: 9
Provided by: cemc2Math
Category:
Tags: miniproject | guis | java

less

Transcript and Presenter's Notes

Title: GUIs in the Java MiniProject


1
GUIs in the Java Mini-Project
  • Imperial Oil Summer Institute for Computer
    Studies Educators
  • August 2006.

2
The Model/View/Controller Paradigm
  • Separate the application logic (model) from the
    UI (view)
  • The view handles all interactions with the user
  • The model keeps track of the state of the
    application
  • The view knows everything about the model
  • Contains a reference to the model object
  • Implements the IView interface
  • Automatically adds itself to the list of views in
    the model
  • The model only knows that there may be some views
    looking at it
  • Whenever something significant changes in the
    state of the game, call the updateAllViews()
    method

3
What is an Interface?
  • An interface defines a set of methods
  • It is like defining a data type
  • No instance variables, no constructor
  • It will be implemented by a class
  • It is a contract by the class to implement all
    methods declared within the interface
  • The class may implement more methods beyond those
    defined in the interface
  • It may be used to simulate multiple inheritance
  • Examples
  • IView
  • Implemented by all GUI elements viewing the model
  • ActionListener
  • Implemented by inner classes defining listeners
    within the GUI

4
MVC in the Java Mini-Project
  • Model - Library.java
  • in the mini-project, the state is recorded in the
    queryResult instance variable
  • View - LibraryGUI.java
  • updateView() displays the value of queryResult
    in the text area
  • Controller - MiniProjectMain.java
  • Weak coupling (optional)
  • limited method interactions between the model and
    the model and the view
  • the model only calls the updateView() method
  • the view only calls the doCommand() method

5
Creating a GUI
  • Import Java libraries -
  • javax.swing., java.awt., java.awt.event.,
  • Construct a frame object
  • Construct a model object
  • Construct a JPanel object with the model as a
    parameter
  • Declare components
  • Instance variables
  • Choose, construct and set the layout
  • Examples FlowLayout, GridLayout, BorderLayout,
    BoxLayout
  • Add components
  • Attach listeners
  • Implement the listeners
  • Inner class
  • Implements an interface

6
What is a Listener?
  • Objects which are notified when a particular
    event occurs
  • Customized reaction to the event, provided by the
    GUI designer
  • Examples of events choosing a button, entering
    text, mouse actions, window actions
  • Standardized set of methods (an interface) that
    the listener must implement
  • will be called by the system automatically
  • Must be attached to a GUI component
  • Has an Event object passed as a parameter
  • evt.getSource()
  • returns the object involved in the event
  • evt.getActionCommand()
  • returns the text associated with object involved
    in the event

7
Examples of GUI in the Mini-Project
  • GUI components
  • JButton
  • JLabel
  • JPanel
  • JTextArea
  • JScrollPane
  • Layout
  • GridLayout (top panel)
  • FlowLayout (bottom panel - default layout)
  • BoxLayout (whole)
  • Listeners/Inner Classes
  • ClearButtonListener
  • LookupListener
  • CheckOutListener

8
Building the GUI in the Mini-Project
  • Declare components
  • private JButton lookupButton new
    JButton("Lookup")
  • Choose, construct and set the layout
  • entryPanel.setLayout(new GridLayout(3, 3,))
  • Add components
  • entryPanel.add(this.lookupButton)
  • Attach listeners
  • this.lookupButton.addActionListener(new
    LookupListener())
  • Implement the listeners
  • private class LookupListener implements
    ActionListener
  • public void actionPerformed(ActionEvent e)
  • model.doCommand(Library.LOOKUP
    _TITLE, enterTitle.getText())
  • //actionPerformed
Write a Comment
User Comments (0)
About PowerShow.com