4/8/2009 2:56 PM OUTLINE OUTLINE Assertions and Loop Assertions and Loop Invariants Invariants GUI Programming GUI Programming Event E Event-Oriented E t t O i O i Oriented t d t d Programming Programming GUI – GUI – first look first look Reasoning about Programs: Reasoning about Programs: Assertions and Loop Assertions and Loop Assertions and Loop Assertions and Loop Invariants Example Invariants Example Invariants Invariants Assertions: logical statements about a Assertions: logical statements about a program that are claimed to be true; program that are claimed to be true; generally written as a comment generally written as a comment Preconditions and Preconditions and postconditions postconditions are are assertions assertions A loop invariant is an assertion A loop invariant is an assertion Helps prove that a loop meets it specification Helps prove that a loop meets it specification True before loop begins, at the beginning of True before loop begins, at the beginning of each repetition of the loop body, and just after each repetition of the loop body, and just after loop exit loop exit Testing Testing Intro to Event Intro to Event- -Oriented Oriented Programming Programming Test Test drivers and stubs are tools used drivers and stubs are tools used Batch- Batch -Processing Processing in testing in testing Request Request- -Response Programming Response Programming Test drivers exercise a method or class Event- Event -Oriented Programming Oriented Programming Stubs stand in for called methods Stubs stand in for called methods 1
4/8/2009 2:56 PM GUI Programming Concepts GUI Programming Concepts GUI GUI - - first look: first look: Graphical Components Graphical Components conventional ( conventional (request request- -response response) ) programming programming: : button button menus menus title title menu menu combo box combo box sequence of operations is determined bar bar bar bar by the program what you want to happen, happens when you y pp , pp y want it event- event -oriented oriented programming: programming: sequence of operations is determined scroll scroll by the user’s interaction with the bars bars application’s interface anything that can happen, happens at any time GUI Programming GUI Programming Graphical Components – Graphical Components – Concepts in Java Concepts in Java more… more… Java GUI ("Swing") has Java GUI ("Swing") has components components Radio Radio Text Text Check Check Button Button Label Label Windows GUI has Windows GUI has controls controls field field Box Box Button Button Unix GUI has Unix GUI has widgets widgets examples examples: labels buttons check examples: labels, buttons, check examples : labels buttons check : labels, buttons, check boxes, radio buttons, text input boxes, radio buttons, text input boxes, pull down lists boxes, pull down lists Combo Box Combo Box Java Swing components Java Swing components: : JLabel JLabel, , JButton JButton, , JCheckBox JCheckBox, , JRadioButton JRadioButton, , JTextField JTextField, , JTextArea JTextArea, , JComboBox JComboBox AWT vs. Sw ing (1) AWT vs. Sw ing (1) Java GUI history: the Java GUI history: the AWT: Abstract Windowing Toolkit AWT: Abstract Windowing Toolkit AWT AWT import java.awt.* import java.awt.* AWT drawing uses "native peers" -- AWT drawing uses "native peers" -- creating creating an AWT button creates a native peer (Unix, an AWT button creates a native peer (Unix, AWT(JDK 1.0, 1.1): AWT(JDK 1.0, 1.1): Mac, Win32) button to put on screen, and Mac, Win32) button to put on screen, and Abstract Window Toolkit Abstract Window Toolkit then tries to keep the AWT button and the then tries to keep the AWT button and the then tries to keep the AWT button and the then tries to keep the AWT button and the package package: java.awt, : java.awt, java.awt.event java.awt.event peer in sync. peer in sync. Advantage Advantage: an AWT app has the "native" : an AWT app has the "native" heavyweight components using heavyweight components using appearance for buttons etc. since there are appearance for buttons etc. since there are native GUI system elements native GUI system elements in fact native buttons on screen. in fact native buttons on screen. used for applets until most used for applets until most Disadvantage Disadvantage: very hard to implement in a : very hard to implement in a browsers supported JRE 1.2 browsers supported JRE 1.2 reliable way reliable way -- -- keeping peers consistent on keeping peers consistent on all platforms. all platforms. 2
4/8/2009 2:56 PM AWT vs. AWT vs. Sw ing (2) Sw ing (2) Sw ing Components in Sw ing Components in Swing is a new widget toolkit Swing is a new widget toolkit Java Java for Java (came with Java2) for Java (came with Java2) advanced GUI support. e.g. drag advanced GUI support. e.g. drag- - import import javax.swing javax.swing.* .* and- and -drop drop Extends AWT (provides a more Extends AWT (provides a more package names: p p package names: javax.swing g g javax.swing, j j g g, , , sophisticated set of GUI components) sophisticated set of GUI components) p p p p ) ) javax.swing.event javax.swing.event Standard dialog boxes, tooltips, … Standard dialog boxes, tooltips, … components inherit from components inherit from Look Look- -and and- -feel, skins feel, skins JComponent JComponent Event listener Event listener components are added to a top- components are added to a top - Swing inherits from Swing inherits from AWT AWT level container: JFrame level container: JFrame, , JDialog JDialog, , AWT still used for events, layouts AWT still used for events, layouts or or JApplet JApplet. . Sources on the Internet: Sources on the Internet: running a Sw ing application running a Sw ing application java - java -Dswing.aatext Dswing.aatext=true =true Application Programming Interface: Application Programming Interface: MySwingClass MySwingClass http://java.sun.com/javase/6/docs/api/ http://java.sun.com/javase/6/docs/api/ the option sets the system property "swing.aatext" to "true" to enable Swing API documentation Swing API documentation anti-aliasing for every JComponent ti li i f JC t http://java.sun.com/javase/6/docs/techn http://java.sun.com/javase/6/docs/techn javaw runs a GUI without the runs a GUI without the otes/guides/swing/ otes/guides/swing/ javaw console console window window Swing architecture Swing architecture http://java.sun.com/products/jfc/tsc/arti http://java.sun.com/products/jfc/tsc/arti cles/architecture/ cles/architecture/ Event- Event -driven Programming driven Programming Event- Event -driven Paradigm in driven Paradigm in Normal (control flow- Normal (control flow -based) programming based) programming Java Java Approach Approach During implementation During implementation Start at main() Start at main() Implement Implement event listeners event listeners for each event for each event Continue until end of program or exit() Continue until end of program or exit() Usually at least one event listener class per Usually at least one event listener class per Event- Event -driven programming driven programming widget widget Unable to predict time & occurrence of Unable to predict time & occurrence of At run time At run time event event Register listener object with widget object Register listener object with widget object Approach Approach Java generates Java generates event object event object when events when events occur occur Start with main() Start with main() Java then passes event object to event listener Java then passes event object to event listener Build GUI Build GUI Await events (& perform associated Await events (& perform associated computation) computation) 3
Recommend
More recommend