原创

マウスボタンをクリックしても読み取られない

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

(提案に沿って編集)

プログラミングプロジェクトのシードソリューションを実装しようとしています。

  • マウスクリックを処理するプライベートイベントハンドラー。左クリックでポイントを追加し、右クリックでポイントを削除し、その後の最大ポイントセットを再計算します。

私の到来 (またはエラーを絞り込む場所):

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();
    }

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