Java Reference
In-Depth Information
￿ e.getModifiersEx()
Returns a button mask showing the mouse buttons that are
pressed when the event e occurred. Should be called only in the mouseDragged method
of a mouse motion listener.
￿ (e.getModifiersEx() & MouseEvent.BUTTON1 DOWN MASK)!=0)
True if the left
mouse button is pressed, where e is the event. Should appear only inside the
mouseDragged method of a mouse motion listener. Note that button 1 is the left
mouse button, button 2 is the middle, while button 3 is the right mouse button.
Creates a menu bar object using Swing.
JMenuBar bar= new JMenuBar();
Adds the menu bar to the frame. Since every window
canhaveatmostonemenubar,the setJMenuBar method is used instead of the add
Menu m = new Menu("File");
Creates a new File menu.
￿ bar.add(m);
Adds the menu m to the menu bar. One can add multiple menus to
the same menu bar.
￿ MenuItem mi = new MenuItem("Open...");
Creates the menu item “Open...”.
￿ m.add(mi);
Adds the menu item to the menu.
￿ a.contains(b)
. Returns true if the element b belongs to the ArrayList a .
10.7 Important Points
1. There are four types of nested classes: nested static, inner, local, and local anonymous.
A nested static class is a static class within a class. The name of the outer class must
be used when creating an instance of the class outside the outer classes. Nested static
classes have access to only the static variables of the outer class. Inner classes are
classes within classes, where an inner class always has a reference to the outer object
and can therefore access the variables of the outer class. A local class is a class within
a method. It only has access to the final variables of the method.
2. Create an anonymous inner class only when the class will not be reused. That is, an
object is created from a class and no other objects will need to be created from this
class again.
3. When handling an event, one needs to be always aware of the event source, the event
listener, and the event object. While the event object is created by Java, the event
source and the event listener must be created by the programmer. The event listener
must belong to a class that implements the appropriate interface so that the event
source can call the correct methods.
4. After creating the event source, do not forget to register the event source with the
event listeners. Write eventSoure.addXListener(eventListener) ,where X should
be substituted with the type of event (e.g., Key, Mouse, etc.).
Search WWH ::

Custom Search