CIS279-BASFORD-06/11/01
Java Programming
CIS279
Tori Basford
tori@bly.net
Assignment(s):
None.
Exercise(s):
- See handout (Exercise 7: ActionEvent and MouseMotionEvents)
ColorBar and CBTest:
The CBTest program should:
- create a frame and content pane
- create 3 ColorBars
- add the ColorBars to the content pane
- set the Frame to be visible
- add the window listener (Pima.CLOSER)
The Class ColorBar should:
- extend JPanel, thus the ColorBar object this is basically a JPanel
- Create a JScrollBar, a small JPanel, and 2 labels and add them to this
- Add a Listener Event to JScrollBar
Miscellaneous:
A label can be aligned left, right, or center. By default, it is aligned to the left. It can be changed via the label.setHorizontalAlignment(JLabel.alignment) method. The alignment is either LEFT, RIGHT, or CENTER (in all CAPS).
Fonts can be changed via the object.setFont(f) method. f is a Font object, and is defined by new Font(fontname, Font.type, size). The fontname is a string. The font.type is either Font.PLAIN, Font.BOLD, or Font.ITALIC. Size is an integer.
5 Guaranteed Fonts / Standard Java Fonts:
- Serif (probably "TimesRoman")
- SansSerif (probably "Helvetica")
- Dialog
- DialogInput
- MonoSpaced (probably "Courier")
TrueType (windows) fonts sometimes only have a Font.PLAIN setting, and setting them to any other font type will still result in a Font.PLAIN.
As with other objects, you can get the current font type of an object by using the getFont() method.
Events:
A JScrollBar generates an AdjustmentEvent and sends it and the adjustmentValueChanged method of any AdjustmentListeners that are attached to the ScrollBar.
A JButton generates an ActionEvent and sends it to the actionPerformed method of any ActionListeners that are attached to the button.
To add an EventListener to a button:
- Create an ActionListener modified to do what we want.
- Add the ActionListener to one or more buttons.
example:
ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        ...
    }
};
jb.addActionListener(al);
You can find out which button has been pressed by using the listenerevent.getSource method and comparing it to the object to see if it matches.
Moving the mouse generates a MouseEvent. To react to it, use the MouseMotionListener. You can then change the mouseMoved(mouseEvent) and mouseDragged(mouseEvent) in the Listener. You can also get the mouse coordinates by using the getX() and getY().
[ return ]