import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class MouseListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { System.out.println("Mouse Y Coordinate on screen: " + e.getYOnScreen()); } }
import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; public class MouseDragListener extends MouseAdapter { private int initialY; private JFrame frame; public MouseDragListener(JFrame frame) { this.frame = frame; } @Override public void mousePressed(MouseEvent e) { initialY = e.getYOnScreen() - frame.getY(); } @Override public void mouseDragged(MouseEvent e) { int newY = e.getYOnScreen() - initialY; frame.setLocation(frame.getX(), newY); } }In this example, we create a new class called `MouseDragListener` that extends `MouseAdapter`. This listener is used to drag a `JFrame` based on the Y coordinate of the mouse cursor on the screen. The initial Y position of the window is determined when the mouse is pressed, and the new location of the window is calculated based on how far the mouse has been dragged. Overall, the examples demonstrate how the `getYOnScreen` method can be used to manipulate the behavior and appearance of a user interface.