如何防止窗格相交阻塞另一个窗格? javafx

How do i prevent pane intersection blocking another pane? javafx

因此,在示例代码中,我有两个窗格(现在是 A 和 B),其中 A 窗格在 B 上方,我的问题是,如果 A 窗格的边界与 B 窗格的边界相交,则 A 窗格阻塞 B 窗格阻止我与其互动 children 我希望我的示例代码可以更好地解释我想要的内容。

所以我想要实现的是能够与 Rectangle globalPaneR 与窗格的当前边界进行交互。

package Whosebug;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class StcakOverFlow extends Application
{
    public static final Pane uiPane = new Pane();
    public static final Pane global_pane = new Pane();
    private Pane localPane = new Pane(global_pane, uiPane);

    @Override
    public void init()
    {
        Rectangle leftTopR = new Rectangle(25, 25, 50, 50);
        Rectangle rightBottomR = new Rectangle(700, 700, 50, 50);
        Rectangle globalPaneR = new Rectangle(600, 600, 50, 50);
        leftTopR.setFill(Color.RED);
        rightBottomR.setFill(Color.YELLOW);
        globalPaneR.setFill(Color.BLUE);

        globalPaneR.setOnMouseClicked(e -> globalPaneR.setFill(Color.LIME));

        uiPane.getChildren().addAll(leftTopR, rightBottomR);
        global_pane.getChildren().add(globalPaneR);
    }

    @Override
    public void start(Stage stage)
    {
        Scene scene = new Scene(localPane);
        stage.setScene(scene);
        stage.setTitle("Whosebug");
        stage.show();
    }

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

正如您已经注意到的那样,uiPane 位于 global_pane 之上,因此消耗了应该到达 globalPaneR 矩形的所有鼠标事件。在这个例子中,这可以通过几种方式来克服:

选项 1(最通用):将 'blocking' 窗格的 pickOnBounds 属性 值设置为 false

uiPane.setPickOnBounds(false);

文档说 pickOnBoundsProperty:

Defines how the picking computation is done for this node when triggered by a MouseEvent or a contains function call. If pickOnBounds is true, then picking is computed by intersecting with the bounds of this node, else picking is computed by intersecting with the geometric shape of this node. The default value of this property is false unless overridden by a subclass. The default value is true for Region.

所以在这种情况下,这基本上意味着 uiPane 不应消耗鼠标事件,除非鼠标悬停在它的子项(矩形)上。

选项 2:对包含矩形的节点使用 Group 而不是 Pane

public static final Group uiPane = new Group();

这是可行的,因为 Group 没有继承自 Region(就像 Pane 那样),因此 pickOnBounds 属性 已经具有默认值 false.

选项 3:更改窗格的顺序。

显然,这并不总是可行的,因为您可能希望在两个窗格中都收听鼠标事件,并且可能希望 uiPane 的子级位于 global_pane 的顶部。但在这个简单的例子中,它可以像这样重新排序它们:

private Pane localPane = new Pane(uiPane, global_pane);