[英]When adding a second item to my stackpane, the first item loses its Event/MouseOn. Why? How can I fix? JavaFX
我有一個堆疊的窗格。 當我向堆棧窗格添加第二個項目時,兩個都顯示,但我不能再點擊我的第一個項目了。 它變得“無法點擊”。
我在.setonmouse中定義的內容不起作用。 它適用於我的第二個項目。 如果我切換它們在堆棧窗格中的順序,則另一個可以工作,但不能同時工作。
有沒有解決這個問題? 這是我的程序的樣子:
我希望我的'網格'始終居中。 左側有一些按鈕位於列的中間,稍后會有右側的按鈕,並且網格頂部會有按鈕/文本,稍后會在邊距中顯示按鈕/文本。
我希望一切都可以點擊。
你可以使任何Pane“鼠標透明”,這樣它就不會消耗任何點擊事件,並讓它們傳遞到它下面的堆棧。
下面是一些示例代碼...此示例在堆棧中設置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將MouseEvents通過透明節點傳遞給子節點
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.