You need to enable JavaScript to run this app. JavaFX实现鼠标交互驱动的矩形操作功能开发及异常排查
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX实现鼠标交互驱动的矩形操作功能开发及异常排查

异常根因
  • 所有鼠标点击事件统一绑定在根Pane上,无差别触发临时矩形创建逻辑:默认创建的矩形无填充色设置、默认显示为黑色,右键点击也会触发该逻辑,就是你看到莫名生成新黑矩形的原因
  • 事件执行顺序未加前置状态判断:不管是否刚激活跟随状态,只要触发左键点击就直接创建永久落地的蓝块,导致第一次点固定黑块时就生成不符合预期的矩形
  • 旋转状态全局共享绑定,无独立临时矩形状态管理,容易引发节点逻辑混乱
交互逻辑修正方案

严格按照预设交互流程拆分事件绑定,避免无状态触发:

  • 给左上角固定黑块单独绑定点击事件,仅负责切换跟随激活状态,不嵌套其他逻辑
  • 新增isFollowing全局状态标记,只有该状态为true时,才响应跟随过程中的右键旋转、左键落地操作
  • 临时跟随矩形单独设置为半透明蓝色,和落地的纯蓝色永久矩形做视觉区分
  • 移除原代码中无意义的translate偏移绑定,直接通过更新矩形的x/y坐标实现鼠标跟随,逻辑更简单
修正后完整可运行代码
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {
    // 固定尺寸参数
    private static final int RECT_WIDTH = 50;
    private static final int RECT_HEIGHT = 100;
    private static final double HALF_WIDTH = RECT_WIDTH / 2.0;
    private static final double HALF_HEIGHT = RECT_HEIGHT / 2.0;

    // 交互状态
    private Rectangle fixedTriggerRect; // 左上角固定黑块
    private Rectangle tempFollowRect;   // 跟随鼠标的临时矩形
    private boolean isFollowing = false; // 是否处于跟随激活状态
    private double currentRotateAngle = 0; // 当前旋转角度

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();
        Scene scene = new Scene(root, 500, 500);

        // 初始化固定黑色触发块
        fixedTriggerRect = new Rectangle(10, 10, RECT_WIDTH, RECT_HEIGHT);
        fixedTriggerRect.setFill(Color.BLACK);
        fixedTriggerRect.setCursor(Cursor.HAND);
        root.getChildren().add(fixedTriggerRect);

        // 固定黑块的点击逻辑:切换跟随状态
        fixedTriggerRect.setOnMouseClicked(event -> {
            if (event.getButton() == MouseButton.PRIMARY) {
                if (isFollowing) {
                    // 退出跟随:移除临时矩形
                    root.getChildren().remove(tempFollowRect);
                    tempFollowRect = null;
                    isFollowing = false;
                } else {
                    // 激活跟随:新建临时半透蓝矩形
                    tempFollowRect = new Rectangle(
                            event.getX() - HALF_WIDTH,
                            event.getY() - HALF_HEIGHT,
                            RECT_WIDTH,
                            RECT_HEIGHT
                    );
                    tempFollowRect.setFill(Color.color(0, 0, 1, 0.5));
                    tempFollowRect.setRotate(currentRotateAngle);
                    root.getChildren().add(tempFollowRect);
                    isFollowing = true;
                }
                event.consume();
            }
        });

        // 全局鼠标移动:仅在跟随状态下更新临时矩形位置
        root.setOnMouseMoved(event -> {
            if (isFollowing && tempFollowRect != null) {
                tempFollowRect.setX(event.getX() - HALF_WIDTH);
                tempFollowRect.setY(event.getY() - HALF_HEIGHT);
            }
        });

        // 全局鼠标点击:处理旋转、落地逻辑
        root.setOnMouseClicked(event -> {
            if (!isFollowing) return;

            // 右键:临时矩形旋转90度
            if (event.getButton() == MouseButton.SECONDARY) {
                currentRotateAngle += 90;
                tempFollowRect.setRotate(currentRotateAngle);
                event.consume();
                return;
            }

            // 左键:落地绘制矩形,保留跟随状态
            if (event.getButton() == MouseButton.PRIMARY) {
                if (event.getTarget() != fixedTriggerRect) {
                    Rectangle landedRect = new Rectangle(
                            event.getX() - HALF_WIDTH,
                            event.getY() - HALF_HEIGHT,
                            RECT_WIDTH,
                            RECT_HEIGHT
                    );
                    landedRect.setFill(Color.BLUE);
                    landedRect.setRotate(currentRotateAngle);
                    root.getChildren().add(landedRect);
                    event.consume();
                }
            }
        });

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
交互效果匹配校验
  • 点击固定黑块:激活跟随状态,生成半透明蓝色临时矩形,自动跟随鼠标移动,固定黑块位置保持不变
  • 跟随状态下右键点击:临时矩形旋转90度,不生成任何新节点
  • 跟随状态下左键点击空白区域:在当前位置生成纯蓝色永久落地矩形,临时矩形保持跟随状态
  • 再次左键点击固定黑块:移除临时跟随矩形,退出跟随状态

内容的提问来源于stack exchange,提问作者Kresljedos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.09 06:54:04