7

我有一个堆栈窗格。当我将第二个项目添加到我的堆栈窗格时,两者都会显示,但我无法再单击我的第一个项目。它变得“不可点击”。

我在 .setonmouse 中定义的任何内容都不起作用。它适用于我的第二个项目。如果我切换它们在堆栈窗格中的顺序,另一个可以工作,但不能同时工作。

有解决办法吗?这是我的程序的样子:

我希望我的“网格”始终居中。左边有按钮在列中居中,稍后会有按钮在右边,网格顶部会有按钮/文本,稍后也会有边距中的按钮。

我希望一切都是可点击的。

http://img688.imageshack.us/img688/6025/examplerg.png

4

2 回答 2

4

StackPane按 Z 顺序订购项目:后者高于前者。所以,你的第二个项目得到了所有的鼠标点击,第一个(被第二个覆盖)没有得到任何东西。

对于您描述的布局,您可以使用 BorderPane:

public void start(Stage stage) throws Exception {
    BorderPane root = new BorderPane();
    root.setCenter(new Rectangle(100,100, Color.RED));
    root.setLeft(new Rectangle(10,10, Color.BLUE));
    root.setRight(new Rectangle(10,10, Color.CYAN));

    stage.setScene(new Scene(root,300,300));

    stage.show();
}
于 2012-03-28T09:21:50.170 回答
4

您可以使任何窗格“鼠标透明”,这样它就不会消耗任何点击事件,并让它们传递到它下面的堆栈。

这是一些示例代码...此示例在堆栈中设置了 4 个窗格,开始时只有 mainPane 接受点击。

StackPane rootPane = new StackPane();
VBox mainPane = new VBox(80);

BorderPane helpOverlayPane = new BorderPane();
helpOverlayPane.setMouseTransparent(true);

Canvas fullScreenOverlayCanvas = new Canvas();
fullScreenOverlayCanvas.setMouseTransparent(true);

VBox debugPane = new VBox();
debugPane.setAlignment(Pos.BASELINE_RIGHT);
AnchorPane debugOverlay = new AnchorPane();
debugOverlay.setMouseTransparent(true);
debugOverlay.getChildren().add(debugPane);
AnchorPane.setBottomAnchor(debugPane, 80.0);
AnchorPane.setRightAnchor(debugPane, 20.0);

rootPane.getChildren().addAll(mainPane, fullScreenOverlayCanvas, debugOverlay, helpOverlayPane);

现在,当您想使用画布在顶部绘图时,请确保仅针对该堆栈将鼠标透明更改为 false,并保持其顶部的所有窗格鼠标透明。

fullScreenOverlayCanvas.setMouseTransparent(false);
debugOverlay.setMouseTransparent(true);
fullScreenOverlayCanvas.setVisible(true);

doSomethingWithCanvasThatNeedsMouseClicks();

PS 我对我拥有的代码进行了一些编辑,因此它可能无法按原样运行。此外,请参阅此处仅使部分窗格透明的讨论: JavaFX Pass MouseEvents through Transparent Node to Children

于 2014-01-11T12:23:45.100 回答