原创

单击时鼠标按钮不被读取

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

(Edited, per suggestion)

Trying to implement a coding solution for a programming project.

  • A private event handler that handles mouse clicks that adds a point with a left click, removes a point with a right click and recomputes the maximal point set afterward.

My implementation (or where I've narrowed down the errors):

public class PointPane extends Pane {
    private final ArrayList<Point> points;

    public PointPane(ArrayList<Point> points) {
        this.points = points;
        drawPoints();
        this.setOnMouseClicked(this::handleMouseClick);
    }

    // this is the section with the issue

    private void handleMouseClick(MouseEvent event) {

        // after entry it should be checking for the left or right mouse button, but
        // it does not enter either the if or the else if and goes directly to
        // drawPoints();

        if (event.isPrimaryButtonDown()) { // Left click
            points.add(new Point(event.getX(), event.getY()));

        } else if (event.isSecondaryButtonDown()) { // Right click            
            Point pointToRemove = null;
            for (Point point : points) {
                if (Math.abs(point.getX() - event.getX()) 
                        < 5 && Math.abs(point.getY() - event.getY()) < 5) {
                    pointToRemove = point;
                    break;
                }
            }
            if (pointToRemove != null) {
                points.remove(pointToRemove);
            }
        }
        drawPoints();
    }

正文到此结束
热门推荐
本文目录