我在堆栈中同时拥有一个SplitPane和一个Pane。窗格位于堆栈中SplitPane的下面,中间的SplitPane是透明的AnchorPane。我的目的是要触发一个事件,当我单击中心窗格区域内的某个位置时,该事件会在基础窗格上绘制一个圆圈,但是我无法通过单击窗格来触发该事件。
我已在此处阅读有关此内容的文章,大多数解决方案包括MouseTransparent和PickOnBounds。我已经尝试过在中央AnchorPane,其标签子项以及需要无用绘制圆圈的基础窗格中对这些属性的每种组合的感觉。欢迎任何帮助! :)
这里是场景构建器中物理布局和层次结构的两幅图像。蓝色显示为基础窗格。可见是因为中央AnchorPane是透明的。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS84Q1k0ZS5wbmcifQ==” alt =“图片1”>
无法分辨您做错了什么。这是一个有效的小例子:
控制器类:
package sample;
import javafx.fxml.FXML;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
public class Controller {
@FXML
private Pane backgroundPane;
@FXML
public void handleOnCenterPaneMousePressed() {
Ellipse ellipse = new Ellipse();
ellipse.setRadiusX(50);
ellipse.setRadiusY(50);
ellipse.setFill(Color.BLACK);
ellipse.setLayoutX((backgroundPane.getWidth() / 2) );
ellipse.setLayoutY((backgroundPane.getHeight() / 2));
backgroundPane.getChildren().add(ellipse);
}
}
FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<StackPane prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Pane fx:id="backgroundPane" style="-fx-background-color: blue;" />
<SplitPane dividerPositions="0.15, 0.85" style="-fx-background-color: rgba(255,255,255,0);">
<items>
<AnchorPane style="-fx-background-color: tomato;" />
<AnchorPane onMousePressed="#handleOnCenterPaneMousePressed" style="-fx-background-color: rgba(255,255,255,0);" />
<AnchorPane style="-fx-background-color: tomato;" />
</items>
</SplitPane>
</children>
</StackPane>
预览: