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 Design9.1From Using to Programming GUIsz Extend model of "keep it simple" in code to GUI¾ Bells and whistles ok, should be 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 Design9.2javax.swing, events, and GUIsz GUI programming requires processing events¾ There’s no visible loop in the program¾ Wire up/connect widgets, some generate events, some process events• Pressing this button causes the following to happen¾ We want to do practice “safe wiring”, meaning?z 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?z How do we cope with widget complexity?Software Design9.3JComponent/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 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 (awt counterparts are Frame and Applet)¾ A Jpanel is a JComponent that is also a Container• Holds JComponents, for example and is holdable as wellSoftware Design9.4What 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, component¾ What about parent references?¾ What about child references?z In java, a parent is responsible for painting its children¾ For “paint” think draw, arrange, manage, …Software Design9.5Widget 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 itself¾ 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 Design9.6BorderLayout (see Browser.java)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 Design9.7Action 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 Design9.8Adding 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 Design9.9A 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 Design9.10Using 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 programsz 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 Design9.11Listenersz 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/consumedz 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 Design9.12Listeners and Adaptersz MouseListener has five methods, KeyListener has three¾ What if we’re only interested in one, e.g., key pressed or mouse pressed?• As interface, we must implement all methods as no-ops• As adapter we need only implement what we wantz Single inheritance can be an annoyance in this situation¾ Can only extend one class, be one adapter, …z What about click/key modifiers, e.g., shift/control/left/both¾ Platform independent, what about Mac?Software Design9.13From Browser/Memory to OOGAz


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?