Review notes

Characteristics of Java

how java works

Comments

There are 3 differenet kind of comments.

main

In more detail :

System.out.println(String s)

class

Identifies

Identifies are used to name class, methods, variables.
Not all characters can form an indentify. In below are the basic rules
  1. An identify is a squence of letters , digits, dollar sign $, underscore _.
  2. It cannot begin with a digit.
  3. It cannot be any reserved keywords like , public, class, void ....
  4. Actually it can be in other language (other than English) if you use unicode.
  5. They are case senstive . i.e. myName is different from MyName.

Primitive type

int

float and double

long

char

boolean

Arithemetic

Flow control

Array

String

class and inheritance

You have to know all the material in my handout. This includes

Swing

Layout

BorderLayout

  • You should remember all the constructor.
  • Use BorderLayout.NORTH, BorderLayout.SOUTH,... to specific the direction.
  • use add(component) or add(component, BorderLayout.CENTER) to add to the center.
  • use add(component, BorderLayout.NORTH) etc to add to the north
  • use remove(int) to remove
  • JFrame

    JPanel

    JButton, JLabel

    JFrame

    Useful methods
    JFrame(), JFrame(String title) two useful constructors.
    getContentPane() you getLayout and add, remove through the contentPane.
    setVisible(boolean) set the frame visible
    setSize(int width,int height) set the dimension of the frame
    pack() use the most "prefect" size for the frame.
    setLocation(int x, int y) Set the location of the upper left corner at (x,y). The upper left corner of you screen is (0,0)
    Attention:
    1. setLayout , add and remove has to be used through getContentPane().
      If you add/remove/setLayout without getContentPane(), it will be a compiling error.
    2. the default layout for JFrame is BorderLayout

    JPanel

    JPanel(),JPanel(LayoutManager) constructors
    void add(Component),void add(Component, int), void remove(int) add, remove components
    void setLayout(LayoutManager),LayoutManager getLayout() the meaning is obvious
    Attention
    1. Unlike JFrame, the class use add, remove, setLayout directly.
    2. The default layout is FlowLayout
    3. You can use it as intermediate container to hold other componenets (refer to my notes.
    4. or you can extends it to create some complicated components.

    Layout

    1. Set the layout of JFrame throught getContentPane().setLayout(....)
    2. set the layout of JPanel by setLayout().

    BorderLayout

    constructors
    BorderLayout(), BorderLayout(int hgap,vgap). The gap is 0 if you don't specify them
    add, remove
  • add(Component comp, int direction), remove(int direction): direction can be BorderLayout.NORTH,....
  • add(Component comp): add the CENTER
  • Attention
    You should know how the output look like if you just add components to north, center, east? what is the output if you just add to north, south and center? Try different combinations.

    FlowLayout

    Refer to my handout for 3 constructors.
    The default alignment for FlowLayout is CENTER
    And the default gap is 5.
    you should know the meaning of index in add(comp,index), remove(index)

    GridLayout

    Refer to my handout for 2 constructors.
    you should know the meaning of index in add(comp,index), remove(index)
    You should know what happen when we set row = 0 or col = 0.

    Pass by reference