Feb 26 Tue. Class Notes

Objectives
  Review
     is/has relationship
     interface
   Components
     Button
     Label
  Layout Managers
     FlowLayout
     BorderLayout
     GridLayout

----------------------------------------

Inheritance is an “is a” relationship
          super class
Composition is a “has a” relationship
          member variables

---------------------------------------

In Java, extends keyword describes the inheritance 
or “is a” relationship.
For example:

  class A extends B
  
class A inherits and has access to all the non-private methods or 
variables of class B

----------------------------------------

Java interface is a skeleton which specifies the protocol that a class 
must provide if the class implements that interface
For example:

public interface MyInter {
    String s = “Server”;
    public void print();
    public String getName();
    void setName(String n);
}

All methods are abstract and implicitly public and all variables are 
constants by default in the interface.

Java uses implements keyword to implement interface. A class can 
implement many interfaces.

Interface extends interface, but a class cannot “extends” interface.

For Example:

interface interA{}
interface interB extends interA {}//legal
class A extends interA {}//illegal

class TestInterface implements MyInter {
      void print() {}//even if doing nothing, 
      String getName() {}
      void setName(String s) {}
}

TestInterface class must implement all the methods in MyInter interface, 
otherwise, it must be declared as an abstract class.

----------------------------------------

Java class can extends one super class, but implements many interfaces.

For example:

class Child extends Parent implements InterA {}
class Child extends Parent implements InterA, InterB,                                 InterC {}

----------------------------------------

An abstract method is a method without method body “{}” 
and ended with semicolon “;”.

For example:

void print();
abstract void print();
public void windowClosing(WindowEvent e);
void print() {} // not abstract method
void print(); {} //abstract method

1. A class with an abstract method, must be declared as an abstract class.
2. An abstract class is a class that exists only to be extended. 
3. Abstract classes cannot be instantiated. 

The concrete classes that extend the abstract classes can be 
instantiated.

For example:

public abstract class Employee {
}

Employee class is an abstract class.

----------------------------------------

Event-Driven
Event – When you take an action on a component.
             Like clicking button, typing, moving mouse, etc.
Listener – An object that is interested in an event

Object must be registered as listener in order to receive event

----------------------------------------

Well-known interfaces

ActionListener
ItemListener
WindowListener
KeyListener
MouseListener
MouseMotionListener

----------------------------------------

WindowListener interface has seven abstract methods.

public void windowActivated(WindowEvent e);
public void windowClosing(WindowEvent e);
public void windowClosed(WindowEvent e);
public void windowDeactivated(WindowEvent e);
public void windowDeiconified(WindowEvent e);
public void windowIconified(WindowEvent e);
public void windowOpened(WindowEvent  e);

When your class implements WindowListener interface, you must 
register your class to the Windowistener (addWindowListener(this))
and implement these seven methods.

For Example:
//MyWindow.java
import java.awt.*;
class MyWindow extends Frame {
    public static void main(String[] args) {
         new MyWindow().createWindow(); 
    }
    void createWindow() {
         setTitle(“My first window program”);
         setSize(400,400);
         setVisible(true);
    }
}

MyWindow cannot be closed by clicking the upright "X" icon, because it
dosen't implement WindowListener interface.

//MyWindow2.java
import java.awt.*;
import java.awt.event.*;
class MyWindow2 extends Frame implements
                          WindowListener{
    public static void main(String[] args) {
         new MyWindow2().createWindow(); 
    }
    void createWindow() {
         addWindowListener(this);
         setTitle(“My second window program”);
         setSize(400,400);
         setVisible(true);
    }

    public void windowActivated(WindowEvent e){}
    public void windowClosing(WindowEvent e) {
            System.exit(0);
    }
    public void windowClosed(WindowEvent e){}
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e){}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e){}
}

MyWindow2 can be closed because it implements WindowLister interface and
registers itself to be a window listener.(addWindowListener(this))

----------------------------------------

You have many ways to implement interfaces

Adapter class (prewritten classes)
Anonymous class
Inner class (nested class)
Independent class

----------------------------------------

Components
Java’s building blocks for creating graphical user interfaces(GUI).

Three categories(AWT):
Visual components: 11
Container components: 4
Menu components: 4

NOTE: They are not exhausted
----------------------------------------

Visula Components (11)
Button
Canvas
Checkbox
Choice
FileDialog
Label
List
ScrollPane
Scrollbar
TextArea
TextField

----------------------------------------

Container Components(4)

Applet
Frame
Panel
Dialog

----------------------------------------

Menu Components(4)

Menu items
Check-box menu items
Separators
Menus

----------------------------------------
Common methods

Implemented by all the visual and comainer components:

getSize()—returns the size of a component(Dimension)
setForeground(Color c) /setBackground(Color c)
setFont(Font f)
setEnabled(true)
setSize(int width, int height) 
setBounds(int x, int y, int width, int height)
setVisible(true)
setLocation(int x, int y)

----------------------------------------

Object
    Component
       Button
       
Button();
Button(String label);
setLabel(String label);
getLabel();
addActionListener(ActionListener a);

Button btn = new Button(“Button 1”);
btn.addActionListener(this);

----------------------------------------

Object
    Component
        Label
        
Place a text in a container
Label();
Label(String text);
Label(String text, int alignment);
getText(); 
setText()

Label lbl = new Label(“Label 1”);

----------------------------------------

Layout Manager
An object that can control the position and size of screen components.

FlowLayout
GridLayout
BorderLayout
CardLayout
GridBagLayout
BoxLayout

----------------------------------------

          Object
          Component
          Container
       Panel      Window
       Applet     Frame  
     FlowLayout   BorderLayout

Panel and Applet have FlowLayout manager by default
Frame and Dialog have BorderLayout manager by default

Applet and Frame are called top-level windows

----------------------------------------

Panel class
Panel is a container which can be used to contain another components 
or Panel is a surface on which you can place components
Panel can be placed inside another Panel.
By default, Panel has FlowLayout manager
Constructors:
    Panel();
    Panel(LayoutManager layout);

----------------------------------------

FlowLayout
Arranges components in horizontal rows, honors a component’s preferred size.(natural size)
Fits as many components as possible into the top row and spills the remainder into a second row.
Appears left to right, in the order in which they are added to their container. 
Constructors:
    FlowLayout(); 
    FlowLayout(int align);
    FlowLayout(int align, int hgap, int vgap);

----------------------------------------

BorderLayout
Divides its territory into five regions.
One region can only contain one component
Honors the preferred height of the North and South comps.
Honors the preferred width of the East and West comps.
Constructors:
        BorderLayout();
        BorderLayout(int hgap, int vgap);
        
Constraints (String)
BorderLayout.NORTH,
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER
etc.

----------------------------------------

GridLayout
Subdivides its territory into a matrix of rows and columns equally.
Conponents are positioned left to right across each row in sequence
Ignores a component’s preferred size
Layout component in a given space

Constructors:
     GridLayout()
     GridLayout(int rows, int cols)
     GridLayout(int r, int c, int hgap, int vgap)
     getters/setters
     
----------------------------------------

Null Layout Manager

setLayout(null);
    setBounds()
    setSize()
    setLocation()

----------------------------------------

Answers to Chapter 11 Section A

1. d     5. c     9. b     13. d     17. a
2. c     6. a     10. a     14. c     18. b
3. c     7. a     11. b     15. a     19. c
4. b     8. b     12. c     16. b

----------------------------------------

Lab Goals

Download MyWindow3.java
Compile the code and fix a problem
Run MyWindow3.java
    Familiar with components
    Experience with FlowLayout Manager
    Experience with BorderLayout Manager
    Experience with GridLayout Manager 
Read and Analyze the code
Add Labels to the container
Play with the Layout Managers

----------------------------------------