JavaFX 2 MouseClicked 事件未按应有的方式生成(如在 JAVA 中)

发布于 2024-12-27 18:50:48 字数 1888 浏览 0 评论 0原文

在下面的代码中,我演示了 java 和 javaFX2 之间的差异,因为它涉及 MOUSE_CLICKED 事件的生成,我不知道它是否应该被预期或可以被视为错误。

似乎在 JavaFX 2.0 中,您可以按下鼠标按钮,移动鼠标,只要您愿意,然后当您释放按钮时,将触发 mouseClicked 事件。与 JAVA 相反,如果单击鼠标按钮后,移动鼠标然后释放按钮,则不会触发 MouseClicked 事件。

为了证明这一点,请尝试以下代码,其中单击鼠标时会在单击点绘制一个矩形。即使按下左键,移动鼠标,然后释放按钮,也会绘制矩形(在释放鼠标按钮的位置)...

public class MouseClickTester extends Application {

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    final Group root = new Group();
    Rectangle rect = new Rectangle(0, 0, 300, 300);
    rect.setFill(Color.RED);
    rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent me) {
            Rectangle yellowRect = new Rectangle(me.getSceneX(), me.getSceneY(),10,50);
            yellowRect.setFill(Color.YELLOW);
            root.getChildren().add(yellowRect);
        }
    });

    root.getChildren().add(rect);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}
}

以下代码执行相同的操作,但使用简单的 Java。但这一次,如果您在按下左键的同时移动鼠标,然后释放鼠标,则不会绘制任何矩形,因为不会触发 mouseClicked 事件。

public class FrameClass extends JFrame{

public FrameClass() throws HeadlessException {
    setPreferredSize(new Dimension(300,300));
    final Canvas canv = new Canvas();
    canv.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            canv.getGraphics().drawRect(e.getX(), e.getY(), 10, 50);
        }

    });

    getContentPane().add(canv);
    pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {
    new FrameClass();
}
}

那么问题本身是:如何证明这种差异是合理的?谁的行为正确,谁的行为错误?之后,如何在 javaFX 2,0 中模拟 mouseClicked 使其行为与 Java 中相同?

感谢您的帮助!

附言。进口货不见了!

In the following code I demonstrate a difference between the java and javaFX2 as far as it concerns generation of a MOUSE_CLICKED event which I do not know if it should be expected or can be considered as bug.

It seems that in JavaFX 2.0 you can press the mouse button, move the mouse for as long as you like and then when you release the button a mouseClicked event will be fired. In contrast to JAVA where if after clicking the mouse button, you move the mouse and then release the button, the MouseClicked event won't be fired.

To prove this try the following code, where when clicking the mouse a rectangle is drawn at the click point. Even if you press the left button, move the mouse and then release the button the rectangle will be drawn (in the point where you released the mouse button)...

public class MouseClickTester extends Application {

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    final Group root = new Group();
    Rectangle rect = new Rectangle(0, 0, 300, 300);
    rect.setFill(Color.RED);
    rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent me) {
            Rectangle yellowRect = new Rectangle(me.getSceneX(), me.getSceneY(),10,50);
            yellowRect.setFill(Color.YELLOW);
            root.getChildren().add(yellowRect);
        }
    });

    root.getChildren().add(rect);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}
}

The following code does the same, but in simple Java. But this time if you move the mouse while the left button is pressed and then you release it no rectangle will be drawn as no mouseClicked event will be fired.

public class FrameClass extends JFrame{

public FrameClass() throws HeadlessException {
    setPreferredSize(new Dimension(300,300));
    final Canvas canv = new Canvas();
    canv.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            canv.getGraphics().drawRect(e.getX(), e.getY(), 10, 50);
        }

    });

    getContentPane().add(canv);
    pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {
    new FrameClass();
}
}

So the question itself: How this difference can be justified? Who is behaving right and who is wrong? And in aftermath, How can I emulate the mouseClicked in javaFX 2,0 to behave the same as in Java?

Thanks for the help!

PS. the imports are missing!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2025-01-03 18:50:48

这是故意的:为同一节点中的每个后续按下和释放操作生成鼠标单击。

不存在哪种行为是对还是错的事实——不同的行为同时使用这两种方法。我们认为当前的行为对于 JavaFX 最有用。

注意:请注意,双击(或多次)单击可验证鼠标在单击之间是否移动。

That's intentionally: mouse click generated for every consequent pressed and released actions in the same node.

There is no truth which behavior is right or wrong -- different tookits uses both approaches. It was decided that current behavior is most useful for JavaFX.

N.B.: please, note, double (and more) click verifies if mouse moved between clicks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文