Java FX – GUIs LESSON NOTE IMPLEMENTING
EVENTHANDLER<MOUSEEVENT> To
make our class able to deal with MouseEvents, we
need to add implements EventHandler<MouseEvent> to
the line public class ClassName
extends Application Once
we do this, we are committing to including the handle method in our program. The easiest way to do this is to right
click on the class name and choose "Add unimplemented methods". TYPES OF MOUSE EVENTS Here
are the types:
sceneName.setOnMouseClicked(this);
sceneName.setOnMouseMoved(this); EVENT DETAILS Inside
the handle method, we can access the x and y location of the mouse at the
time of the event. We can also access
the event type. See in the example
below how that was done. EXAMPLE
1 Here is the code:
EXAMPLE
2 The following example will simply draw a dot at every location that is
clicked. No other mouse events are
monitored.
EXAMPLE
3 Here is another example that allows you to click on squares and they
change colours. The squares are stored
inside a grid that is simply redrawn every time a click occurs. Here is the code:
|
|||
|