Mar 7 Thr. Class Notes
Objectives
Review
Inner /Anonymous class
GUI Design
TextField/ActionListener
Font
FontMetrics
Dimension/Point
How to create Menu
Lab (30pts)
-----------------------------------------------------------
Dimension class
Dimension()
Creates an instance of Dimension with a width of zero and a height of zero.
Dimension(Dimension d)
Creates an instance of Dimension whose width and height are the same as for
the specified dimension.
Dimension (int width, int height)
Constructs a Dimension and initializes it to the specified width and
specified height.
getters/setters
Dimension getSize();
For Example:
to get a button size
Dimension d = btn.getSize();
int btnW = (int)d.getWidth();
int btnH = (int)d.getHeight();
------------------------------------------------------------
Point class
Point()
Constructs and initializes a point at the origin (0, 0) of the
coordinate space.
Point (int x, int y)
Constructs and initializes a point at the specified (x, y) location
in the coordinate space.
Point (Point p)
Constructs and initializes a point with the same location as the
specified Point object.
Point getLocation()
For example:
to set a location of a button
btn.setLocation(x,y);
------------------------------------------------------------
Font class
public Font(String fontName, int style, int size);
getters/setters
Style:
Font.BOLD, Font.ITALIC, Font.PLAIN….
Size: point (1/72 inch)
void setFont(Font f);//button or label text, applet
setFont(new Font(“fontName”, Font.BOLD, 24));
Logical fonts are the five font families defined by the Java
platform which must be supported by any Java runtime environment(JRE):
1. Serif
2. SansSerif
3. Monospaced
4. Dialog
5. DialogInput
AWT: Label and TextField can only use logical fonts
------------------------------------------------------------
FontMetrics class
public FontMetrics(Font f);
public int getLeading()
public int getAscent()
public int getDescent()
public int getHeight()
public int getMaxAscent()
public int getMaxDescent()
------------------------------------------------------------
Java Pull-Down Menu
All items in a menu must belong to the class MenuItem, or
one of its subclasses.
Steps for creating a menu sytem
1. create a menu bar and attach it to the frame
2. create and populate the menu
3. attach the menu to the menu bar.
ActionListener
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==menuSave) {}
else if(ae.getSource()==menuOpen) {}
……
else {doSth() }
}
menuSave.addActionListener(ActionListener al)
implements ActionListener or use inner class or anonymous class
Constructors:
MenuBar()
Menu(String label)
MenuItem(String label)
CheckboxMenuItem(String label)
CheckboxMenuItem(String label, boolean state)
How to:
1. Create a MenuBar
MenuBar mbar = new MenuBar();
2. Create Menus.
Menu file = new Menu(“File”);
3. Add MenuItem to Menu
MenuItem open = new MenuItem(“Open”);
file.add(open); //add actionListener() etc.
4. Add Menu to MenuBar
mbar.add(file);
5. Attach MenuBar to a frame
frame.setMenuBar(mbar);//associate frame to it
6. Add Separator
aMenu.addSeparator();
7. Add sub menu
add another Menu with MenuItem to a Menu
8. Add Short Cut
use MenuShortcut class
------------------------------------------------------------
Lab: create a Color menu and add it to the existing menu system.
import java.awt.*;
import java.awt.event.*;
public class MyMenu extends Frame{
private MenuBar bar;
private Menu file, help;
MyMenu(String title) {
super(title);
createMenu();
}
public static void main(String[] args) {
new MyMenu(“YourName’s Menu");
}
....
omit