Gu iintro(java)


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Gu iintro(java)

  1. 1. GUI Programming in Java Tim McKenna [email_address]
  2. 2. GUI Programming Concepts <ul><li>conventional programming: </li></ul><ul><ul><li>sequence of operations is determined by the program </li></ul></ul><ul><ul><li>what you want to happen, happens when you want it </li></ul></ul><ul><li>event-driven programming: </li></ul><ul><ul><li>sequence of operations is determined by the user ’s interaction with the application’s interface </li></ul></ul><ul><ul><li>anything that can happen, happens at any time </li></ul></ul>
  3. 3. GUI Design Concepts <ul><li>a wealth of information creates a poverty of attention - Herbert Simon </li></ul><ul><li>Principles of good GUI Design </li></ul><ul><li>IBM's Design concepts </li></ul><ul><li>Saul Greenberg's HCI pages </li></ul><ul><li>Tim's HCI notes </li></ul>
  4. 4. GUI Programming Concepts in Java <ul><li>Java GUI (&quot;Swing&quot;) has components </li></ul><ul><li>Windows GUI has controls </li></ul><ul><li>Unix GUI has widgets </li></ul><ul><li>examples: labels, buttons, check boxes, radio buttons, text input boxes, pull down lists </li></ul><ul><li>Java Swing components: JLabel, JButton, JCheckBox, JRadioButton, JTextField, JTextArea, JComboBox </li></ul>
  5. 5. Java GUI history: the AWT <ul><li>AWT(JDK 1.0, 1.1): Abstract Window Toolkit </li></ul><ul><li>package: java.awt, java.awt.event </li></ul><ul><li>heavyweight components using native GUI system elements </li></ul><ul><li>used for applets until most browsers supported JRE 1.2 </li></ul>
  6. 6. Swing in Java <ul><li>Swing(Java 2, JDK 1.2+) </li></ul><ul><li>lightweight components that do not rely on the native GUI or OS </li></ul><ul><li>“ look and feel” of Swing components </li></ul><ul><ul><li>are identical on different platforms </li></ul></ul><ul><ul><li>can be customized </li></ul></ul><ul><li>Swing inherits from AWT </li></ul><ul><li>AWT still used for events, layouts </li></ul>
  7. 7. Swing Components in Java <ul><li>advanced GUI support. e.g. drag-and-drop </li></ul><ul><li>package names: javax.swing, javax.swing.event </li></ul><ul><li>components inherit from JComponent </li></ul><ul><li>components are added to a top-level container: JFrame, JDialog, or JApplet. </li></ul>
  8. 8. running a Swing application <ul><li>java -Dswing.aatext=true MySwingClass </li></ul><ul><ul><li>the option sets the system property &quot;swing.aatext&quot; to &quot;true&quot; to enable anti-aliasing for every JComponent </li></ul></ul><ul><li>javaw runs a GUI without the console window </li></ul><ul><li>e.g. </li></ul>
  9. 9. Basic GUI Programming Steps in Java <ul><li>declare a container and components </li></ul><ul><li>add components to one or more containers using a layout manager </li></ul><ul><li>register event listener(s) with the components </li></ul><ul><li>create event listener method(s) </li></ul>
  10. 10. Basic GUI Programming Concepts in Java <ul><li>Example:,, </li></ul><ul><li>container : a screen window/applet window/panel that groups and arranges GUI components </li></ul><ul><li>GUI component: an object with visual representation </li></ul><ul><li>Swing containers: JFrame, JApplet, JPanel </li></ul><ul><li>AWT containers: Frame, Applet, Panel </li></ul>
  11. 11. GUI Programming: The Java Approach <ul><li>event-driven programming </li></ul><ul><ul><li>a piece of code (i.e. event handler ) is attached to a GUI component </li></ul></ul><ul><ul><li>an event handler is called when an event (e.g. a mouse click) is activated / fired </li></ul></ul><ul><li>The Delegation Event Model in Java </li></ul><ul><ul><li>processing of an event is delegated to an object (the listener) in the program </li></ul></ul>
  12. 12. Event-driven Programming in Java <ul><li>event source: a GUI component that generates / fires an event </li></ul><ul><li>event: a user interaction (e.g. a click on the button) </li></ul><ul><li>event listener: an object that has implemented event handlers to react to an event </li></ul>
  13. 13. Event Handling in Java <ul><li>the delegation event model - event listeners must be registered with an event source in order to receive notification </li></ul>
  14. 14. Event Handling in Java <ul><li>registration of an event listener - write a class that implements an [ event type]Listener interface - create an instance of that class (i.e. an event listener) - register the listener with a GUI component: add[event type]Listener ( <an event listener> ) </li></ul>
  15. 15. Event Handling in Java <ul><li>a listener interface has a list of standard event handlers (i.e. methods) </li></ul><ul><li>API documentation - java.awt.event - event classes - listener interfaces - adapter classes </li></ul>
  16. 16. Event Handling in Java <ul><li>different ways of coding the event listeners </li></ul><ul><li> uses named inner classes. </li></ul><ul><li> uses named inner classes and shows how to consolidate window closing from two different events. </li></ul><ul><li> does not use inner classes. </li></ul>