1 / 29

GUI

GUI. Swing Class Hierarchy. Swing Components. Swing Conatiners. JFrame – top-level window to store components. Swing Conatiners. JPanel – container; can be embedded in JFrame. Layouts. FlowLayout arranges elements in a row elements centered by default within container GridLayout

truda
Download Presentation

GUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI

  2. Swing Class Hierarchy

  3. Swing Components

  4. Swing Conatiners • JFrame – top-level window to store components

  5. Swing Conatiners • JPanel – container; can be embedded in JFrame

  6. Layouts • FlowLayout • arranges elements in a row • elements centered by default within container • GridLayout • subdivides container into cells of identical sizes • components take up all available space of a cell • BorderLayout • subdivides container into 5 areas: N, S, E, W, Center

  7. Layouts 3x3 GridLayout 4x1 GridLayout BorderLayout FlowLayout used to place the 3 panels in the Jframe.

  8. Listeners • Process events • ActionListener (JButton, Timer, JComboBox)‏ • ChangeListener (JSlider)‏ • MouseListener, MouseMotionListener • Listeners are interfaces; must implement ALL specified methods • ActionListener: void actionPerformed(ActionEvent e)‏ • ChangeListener: void stateChanged(ChangeEvent e)‏ • MouseListener: void mouseClicked(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e)‏ • MouseMotionListener: void mouseMoved(MouseEvent e)void mouseDragged(MouseEvent e)

  9. Adapter classes • Convenience classes • server as intermediaries between available interfaces (e.g. listeners) and the user defined classes (e.g. listeners) • make it possible to implement only the “important” methods

  10. Adapter classes • Convenience classes • server as intermediaries between available interfaces (e.g. listeners) and the user defined classes (e.g. listeners) • make it possible to implement only the “important” methods abstract class MouseAdapter implements MouseListener, MouseMotionListener { void mousePressed(MouseEvent e) { // empty body } void mouseReleased(MouseEvent e) { // empty body } void mouseEntered(MouseEvent e) { // empty body } void mouseExited(MouseEvent e) { // empty body } void mouseMoved(MouseEvent e) { // empty body } void mouseDragged(MouseEvent e) { // empty body } } MouseListener methods MouseMotionListener methods

  11. Adapter classes • Convenience classes • server as intermediaries between available interfaces (e.g. listeners) and the user defined classes (e.g. listeners) • make it possible to implement only the “important” methods abstract class MouseAdapter implements MouseListener, MouseMotionListener { abstract void mousePressed(MouseEvent e); abstract void mouseReleased(MouseEvent e); abstract void mouseEntered(MouseEvent e); abstract void mouseExited(MouseEvent e); abstract void mouseMoved(MouseEvent e); asbtract void mouseDragged(MouseEvent e); }

  12. File IO

  13. File IO • PrintWriter – for writing to file; same methods as in System.out • Pretend that all output goes to screen try { PrintWriter output = new PrintWriter(“input-file.txt”); output.println(“Hello”); output.printl(42); output.println(“3.1459”); output.close(); } catch (Exception e) { // report error }

  14. File IO • Scanner – for reading from file; same as in CS111 • Contents is viewed as a stream of characters • Reading stops as soon as appropriate token found; otherwise fails try { Scanner input = new Scanner(new File(“input-file.txt”)); String word = input.next(); int answer = input.nextInt(); double pi = input.nextDouble(); input.close(); } catch (Exception e) { // report error }

  15. Command-Line

  16. Command Line Arguments • Make it possible to send data to the program on execution public static void main(String[] args)‏ { System.out.println(“Parameters given on start-up:”); System.out.println(“Number of params: “ + args.length); for (int i = 0; i < args.length; i++) { System.out.println(args[i]); // i-th parameter value } }

  17. Exceptions

  18. Exceptions • Mechanism for handling unexpected conditions (errors)‏ • Force the programmer to handle error conditions • Allow for separating the logic of the code from error-handling • Sometimes no other option to report the value: • constructor • minElement, maxElement • Example – see FileIO

  19. Exceptions • Can create our own type of exception (should inherit from Exception)‏ class EmptyArrayException extends Exception { public void EmptyArrayException()‏ { super(); } public void EmptyArrayException(String message)‏ { super(message); } }

  20. Exceptions • Example of our own Exception --- throw/throws int minElement(int[] numbers) throws EmptyArrayException { // empty array --- throw an exception if (numbers.length == 0)‏ { throw EmptyArrayException(“Empty array given”); } // // ... compute smallest element ... // }

  21. extends vs. implementsclass vs. interfacemultiple inheritance

  22. Interfaces • An interface specifies a collection of methods • An interface does not have data members or code for methods • A class that implements the interface must provide code (implementation) for all methods listed in the interface • interface RemoteControllable • { • public void play(); • public void stop(); • public void ffwd(); • } • class VCR implements RemoteControllable • { • // must provide code for all methods in RemoteControllable • } • class DVD implements RemoteControllable • { • // must provide code for all methods in RemoteControllable • }

  23. Multiple Inheritance Class Mammal Class Pet String name; //species void setName(String n) { ... ... ... } String name; // pet’s name void setName(String n) { ... ... ... } Class Cat

  24. Multiple Inheritance Class Mammal Class Pet String name; //species void setName(String n) { ... ... ... } String name; // pet’s name void setName(String n) { ... ... ... } Class Cat Which name is inherited? Which setName() is inherited?

  25. Multiple Inheritance Class Mammal Class Pet String name; //species void setName(String n) { ... ... ... } String name; // pet’s name void setName(String n) { ... ... ... } Class Cat Which name is inherited? Which setName() is inherited? • Complex rules required to disambiguate in multiple inheritance • Java does not support multiple inheritance; C++ does

  26. Multiple Inheritance • What if we still want a Cat to behave like a Mammal and Pet • interface Mammal • { • // all methods (behaviors) common to mammals • // no code is specified, just the behavior names (methods) • } • class Pet • { • // description of generic pet • } • class Cat extends Pet implements Mammal • { • // has all behaviors of a Pet – could override some • // must implement all behaviors of Mammal • }

  27. Multiple Inheritance • Can now use Cat objects anywhere Mammal behaviors required or where Pet objects are required • public void hunt(Mammal predator, Mammal prey) • { • // do something; could send a Cat as • // either prey or predator • } • public void doTricks(Pet pet) • { • // do something; could send a Cat for pet • }

  28. Multiple Interfaces Implementation • A Java class can only extend from one other class (single inheritance) • A Java class can implement multiple interfaces – can ambiguity arise?

  29. Multiple Interfaces Implementation • A Java class can only extend from one other class (single inheritance) • A Java class can implement multiple interfaces – no ambiguity since • an interface cannot have data members • an interface cannot have code (implementation) for methods

More Related