DOC PREVIEW
Duke CPS 108 - Lecture

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Software Design5.1From Using to Programming GUIsz Extend model of "keep it simple" in code to GUI¾ Bells and whistles ok, but easy to use and hidez We're talking about software design¾ Not HCI or user-interface design or human factors…¾ However, compare winamp to iTunesz How do we design GUIs¾ Programming, drag-and-drop, …¾ How do we program/connect GUIs?Software Design5.2javax.swing, events, and GUIsz GUI programming requires processing events¾ There’s no visible loop in the program¾ Wire up/connect widgets• Menu, button, text area, spinner, combobox, …• some generate events, some process eventsz Pressing this button causes the following to happen¾ We want to do practice “safe wiring”, meaning?¾ Open-closed principleSoftware Design5.3Building GUIsz We need to put widgets together, what makes a good user-interface? What makes a bad user-interface?¾ How do we lay widgets out, by hand? Using a GUI-builder? Using a layout manager?¾ How do we cope with widget complexity?z Use the Sun Java Tutorial¾ See other online sourcesSoftware Design5.4JComponent/Container/Componentz The java.awt package was the original widget package, it persists as parent package/classes of javax.swing widgets¾ Most widgets are JComponents (subclasses), to be used they must be placed in a Container¾ The former is a swing widget, the latter awt, why?z Container is a component, but contains a collection of components (similar to what design pattern?)¾ Composite Design PatternSoftware Design5.5Composite, Container (continued)z A Container is also a Component, but not all Containers are JComponents (what?)¾ JFrame is often the “big container” that holds all the GUI widgets, we’ll use this and JApplet (awtcounterparts are Frame and Applet)z A JPanel is a JComponent that is also a Container¾ Holds JComponents, for example and is holdableas wellSoftware Design5.6What do Containers do?z A Container is a Component, so it’s possible for a Container to “hold itself”? Where have we seen this?“You want to represent part-whole hierarchies of objects. You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly”.z Composite pattern solves the problem. Think tree, linked list: leaf, composite, componentSoftware Design5.7Parents and Childrenz What about parent references?¾ Does child need to know who contains it?z What about child references?¾ Does parent need to know who it contains?z In Java/Swing, a parent is responsible for painting its children¾ For “paint” think draw, arrange, manage, …Software Design5.8Widget layoutz A Layout Manager “decides” how widgets are arranged in a Container¾ In a JFrame, we use the ContentPane for holding widgets/components, not the JFrame itselfz Strategy pattern: “related classes only differ in behavior, configure a class with different behaviors… you need variants of an algorithm reflecting different constraints…context forwards requests to strategy, clients create strategy for the context”¾ Context == JFrame/container, Strategy == Layoutz Layouts: Border, Flow, Grid, GridBag, Spring, …¾ I’ll use Border, Flow, Grid in my codeSoftware Design5.9BorderLayout (see code)z Default for the JFrame contentpanez Provides four areas, center is “main area” for resizingz Recursively nest for building complex (yet simple) GUIsz BorderLayout.CENTER for adding components¾ Some code uses “center”, bad idea (bugs?)CENTERNORTHSOUTHWESTEASTSoftware Design5.10Action and other eventsz Widgets generate events, these events are processed by event listeners¾ Different types of events for different scenarios: press button, release button, drag mouse, press mouse button, release mouse button, edit text in field, check radio button, …¾ Some widgets “fire” events, some widgets “listen” for eventsz To process events, add a listener to the widget, when the widget changes, or fires, its listeners are automatically notified.¾ Observer/Observable (related to MVC) patternSoftware Design5.11Adding Listenersz In lots of code you’ll see that the Container widget is the listener, so pressing a button or selecting a menu is processed by the Container/Frame’s actionPerformed method¾ All ActionListeners have an actionPerformed method, is this interface/implements or inheritance/extends?¾ Here’s some “typical” code, why is this bad?void actionPerformed(ActionEvent e){if (e.getSource() == thisButton) …else if (e.getSource() == thatMenu)…}Software Design5.12A GUI object can be its own clientz Occasionally a GUI will be a listener of events it generates¾ Simple, but not extendable¾ Inner classes can be the listeners, arguably the GUI is still listening to itself, but …• Encapsulating the listeners in separate classes is betterz Client (nonGUI) objects cannot access GUI components¾ Properly encapsulated JTextField, for example, responds to aGui.displayText(), textfield not accessible to clients¾ If the GUI is its own client, it shouldn’t access textfield• Tension: simplicity vs. generalityz Don’t wire widgets together directly or via controller that manipulates widgets directly¾ Eventual trouble when GUI changesSoftware Design5.13Using inner/anonymous classesz For each action/event that must be processed, create an object to do the processing¾ Command pattern: parameterize object by an action to perform, queue up requests to be executed at different times, support undo¾ There is a javax.swing Event Queue for processing events, this is the hidden while loop in event processing GUI programsSoftware Design5.14Anonymous classesz The inner class can be named, or it can be created “anonymously”¾ For reuse in other contexts, sometimes naming helpful¾ Anonymous classes created close to use, easy to read (arguable to some)Software Design5.15Listenersz Events propagate in a Java GUI as part of the event thread¾ Don’t manipulate GUI components directly, use the event thread¾ Listeners/widgets register themselves as interested in particular events• Events go only to registered listeners, can be forwarded/consumedSoftware Design5.16More on Listenersz ActionListener, KeyListener, ItemListener,MouseListener, MouseMotionListener, …, see java.awt.event.*¾ Isolate listeners as separate classes, mediators between GUI, Controller, Application¾ Anonymous classes can help here tooSoftware


View Full Document

Duke CPS 108 - Lecture

Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?