Download presentation
Presentation is loading. Please wait.
1
Birds perspective on Java GUI Programming Introduction to the major concepts
2
kelk & sh 200402312 Indledende Datalogi Two GUI class-libraries AWT (Abstract Windowing Toolkit) –Came first – has ”Look & Feel” as platform (Windows, Linux, Apple...) Swing –Newer – common L&F across platforms –L&F Programmable –Class names mostly as AWT + ”J” in front –Not a complete replacement for AWT
3
kelk & sh 200402312 Indledende Datalogi Two basic App’s Applications –Based on Frame or JFrame –Run with ”java” – to get a console (nice for debug) –Run with ”javaw” to have no console –Same ”rights” as ”native” programs Applets –Based on Applet or JApplet –Embedded in HTML –Runs in Browser – e.g. IExplorer –Sandbox model for security (restrictions on I/O)
4
kelk & sh 200402312 Indledende Datalogi Applications in Swing Your class ”extends” JFrame (inheritance) - we get a lot for free this way The GUI is ”wired up” in the constructor Components are added to the pane which is found with ”getContentPane()” (in AWT directly added to the Frame) A LayoutManager places components - e.g. ”Border”, ”Grid”, ”Flow”... E.g. in ”main” the class is instantiated
5
kelk & sh 200402312 Indledende Datalogi Application Code Skeleton MyClass extends JFrame { MyClass() { JButton hit = new JButton(”Hit!”); hit. getContentPane().add(hit);....Setup Layout+more components } public static void main(String[] a){ MyClass frame = new MyClass(); frame.pack(); frame.setVisible(true); }}
6
kelk & sh 200402312 Indledende Datalogi From Application to Applet Change constructor into ”void init()” - and remove calls to ”super” if any Remove ”main” completely - no instantiation (done by browser) No setting of tittle Make an HTML-file with tag
7
kelk & sh 200402312 Indledende Datalogi The wellknown console paradigm Client program is in charge Client calls OS when doing I/O etc Client continues after call Typically asks one question at the time and waits for an answer Client OS time
8
kelk & sh 200402312 Indledende Datalogi The Event-Based Paradigm The OS is in charge OS calls fragments of client code - based on events Not easy to predict calling sequences You create code that you never call OS client time
9
kelk & sh 200402312 Indledende Datalogi MVC-pattern: Model-View-Control Model is the data in e.g. attributes/fields or externally - can be complex data-structures. View is the GUI-components setup in the constructor (Buttons, Lists, Checkboxes etc) Control is program-flow - done via event-handlers (With MS MFC View and Control is mixed)
10
kelk & sh 200402312 Indledende Datalogi Listeners & Event-Handlers Appl. Comp.Event ObjectListener Object The OS generates an event with help from the application’s component, and sends it to a handler. A handler is a method on a listener object Your ”listener” is registered at your component – typically in Constructor (Frame) or ”init()” (Applet)
11
kelk & sh 200402312 Indledende Datalogi Listener Groups ActionListener for buttons and menus KeyListener for keyboard MouseListener WindowListener for Window opened, closed etc. There are 4 ways to implement listeners & handlers… following in (personal) prioritized order
12
kelk & sh 200402312 Indledende Datalogi 1. Way: Inner Class Inside the client class a new class is coded Register listener using e.g. ”new MyListener()” It has access to all fields in the outer class There may be several instances of the inner class per instance of the outer class This concept encapsulates the listeners code – without mixing MVC too much
13
kelk & sh 200402312 Indledende Datalogi 2. Way: Application is Listener As with inner-class, all code is in the same file – full access to fields. Register listener using ”this” MVC not as mixed as anonymous classes MVC not as separated as inner classes Need to use ”implements” in order to extend JFrame/Frame/JApplet/Applet....see later slide...
14
kelk & sh 200402312 Indledende Datalogi 3. Way: External Class The listener is coded in it’s own file – exactly as any other class. Register listener using e.g. ”new MyListener()” Control & View is completely separated - maybe too much. No access to private fields of View class.
15
kelk & sh 200402312 Indledende Datalogi 4. Way: Anonymous class The listener is registered with new without a class name at the exact place in the code where the component is inserted. This can be done automatically by many IDE’s This concept encapsulates the code – but mixes control and view It is confusing and difficult to read – drop it!
16
kelk & sh 200402312 Indledende Datalogi Two ways to code listeners To assure that it contains the necessary methods with the right parameters, you can use ”implements” – the drawback is that all methods (actually rather few) must be implemented. Alternatively ”extend” an ”adapterclass” - the drawback is that it’s only possible to extend from one class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.