Feb 28 Thr. Class Notes

Objectives
  Review
     FlowLayout
     BorderLayout
     GridLayout
  Components
     TextField
  Use Adapter Class
  Use an Inner Class
  Use an anonymous Class
  ActionListener
  GUI design

----------------------------------------
Interface Implementations

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

----------------------------------------
public abstract class WindowAdapter implements WindowListener, 
                     WindowStateListener,    WindowFocusListener {
  public void windowClosing(WindowEvent e){ }
   // 7 methods
   //etc.
}

WindowAdapter class for receiving window events,
all methods inside are empty.

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

Inner class example

//MyWindow4.java
import java.awt.*;
import java.awt.event.*;
class MyWindow4 extends Frame {
    public static void main(String[] args) {
        MyWindow4 mw = new MyWindow4();
        mw.createWindow(); 
    }   
    void createWindow() {
        addWindowListener(new CloseMyWindow());
        setTitle("My Fourth Window Program");
        setSize(400,400);
        setVisible(true);
    }
   //inner class
   class CloseMyWindow extends WindowAdapter {
         public void windowClosing(WindowEvent e) {
               System.exit(0);
        }
   }
}
When you compile the code, you will get two classes.
MyWindow4.class
MyWindow4$CloseMyWindow.class

----------------------------------------
Anonymous class
A local class that is declared and instantiated in one statement 
without name. It can be declared to extend another class or to 
implement a single interface.

void createWindow() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
           System.exit(0);
    }
        });
}
The anonymous class can be translated to:

class xxx extends WindowAdapter {
     public void windowClosing(WindowEvent e) {
           System.exit(0);
    }
}

----------------------------------------
Example of anonymous class

//MyWindow5.java
import java.awt.*;
import java.awt.event.*;
class MyWindow5 extends Frame {
    public static void main(String[] args) {
        MyWindow5 mw = new MyWindow5();
        mw.createWindow(); 
    }   
    void createWindow() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
      System.exit(0);
            }
        });
        setTitle("My Fifth Window Program");
        setSize(400,400);
        setVisible(true);
    }
}
When you compile the code, you will get:

MyWindow5.class
MyWindow5$1.class
----------------------------------------
TextField

Constructors
    TextField()
    TextField(int columns)
    TextField(String text)
    TextField(String text, int columns)

setText()
getText()

What is the data type in the TextField? 
    String
How to get data in the TextField? 
    getText()
How to set data in the TextField? 
    setText(“xxx”)

For Example:

TextField principal = new TextField(20);
TextField another = new TextField(“someStr”);

String temp1 = principal.getText();

double pr = Double.parseDouble(temp1);

principal.setText(“”); //clear the content
----------------------------------------

ActionListener Interface

public void actionPerformed(ActionEvent e) {
}

addActionListener(ActionListener al);

ActionEvent:
    Click a button
    Press Return after entering text in a TextField
    Click menu item
---------------------------------------
To check if a button is being clicked in the
public void actionPerformed(ActionEvent e) method:
At least, you can do it in one of the following ways.

  1.  String caption = e.getActionCommand()
       if (caption.equals("xxxx")){}

  2.  Component comp =(Component) e.getSource();
       if (comp == specificBtnInstance) {}
  
  3.  Button btn = (Button)e.getSource();
         if (btn == specificBtnInstance) {}
  
  4.  if (e.getSource()==specificBtnInstance) {}

----------------------------------------
Color class

A final class
Contructors:
    public Color(int r, int g, int b);//r g b range: 0-255
    public Color(int rgb);
    public Color(float r, float g, float b);//r g b range: 0.0-1.0

Get a black color
Color c = Color.black; //lowercase
or 
Color c = Color.BLACK; // uppercase

13 colors:
---------
black    blue    cyan        darkGray
gray     green   lightGray    magenta
orange   pink    red          white
yellow            

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

Color name    Red    Green  Blue
white         255    255    255
lightGray     192    192    192
gray          128    128    128
darkGray       64     64     64
black           0      0      0
red           255      0      0
pink          255    175    175
orange        255    200      0
yellow        255    255      0
green           0    255      0
magenta       255      0    255
cyan            0    255    255
blue            0      0    255

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

Currency Format

import java.text.*;

double num = 8092.326;
String str =NumberFormat.getCurrencyInstance().format(num);
System.out.println(str);//$8,092.33
If num is 8092, it will be $8,092.00

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

Create GUI Application
3 Steps:
1. Compose your GUI by adding components to Container objects. 
2. Setup event handlers to respond to user interaction with the GUI. 
3. Display the GUI

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

GUI Design

Use layout managers to design 
    Calculator
    Mortgage Calculator
    
----------------------------------------
Review points for QUIZ II

1. Variable scope (field, local, parameters)
2. Default values for variables
3. Keywords & Reserved Words
4. Wrapper classes
5. Array length & String length()
6. for loop
7. Questions about your project 1 and project 2

Hints: read class notes

---------------------------------
Lab Goals omitted