Ron*_*ldo 6 java stack label javafx button
如何使标签后面的按钮可以点击?
按键
Button btn = new Button();
//TODO: button's main function, like onPressed, onReleased etc. here...
Run Code Online (Sandbox Code Playgroud)
自定义标签
CustomLabel sp = new CustomLabel("Some texts here"));
//TODO: custom label's main function, like a translucent background etc here...
//main purpose of this is to overlay the button
Run Code Online (Sandbox Code Playgroud)
主要窗格
StackPane mainPane = new StackPane();
mainPane.getChildren.addAll(btn, sp);
Run Code Online (Sandbox Code Playgroud)
这样,自定义标签覆盖的区域变得不可点击.
有没有让按钮仍然可点击,即使覆盖,例如?或者还有另一种方法吗?有点像设置标签在点击时不可见?
编辑:回答Itamar Green的问题..
通过使用此链接中显示的示例:鼠标事件在基础层上被忽略,它似乎仍然无法正常工作.堆叠在图层下的按钮仍然无法点击.
sp.setPickOnBounds(false);
Run Code Online (Sandbox Code Playgroud)
您可以将mouseTransparent顶部绘制的元素的属性设置为true。请注意,这样鼠标事件的节点的所有后代都会被忽略:
@Override
public void start(Stage primaryStage) {
Button btn = new Button("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
System.out.println("Hello World!");
});
Region region = new Region();
region.setStyle("-fx-background-color: #0000ff88;");
region.setMouseTransparent(true);
StackPane root = new StackPane(btn, region);
Scene scene = new Scene(root, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)
注释掉
region.setMouseTransparent(true);
Run Code Online (Sandbox Code Playgroud)
并且按钮将不再以任何方式对鼠标事件做出反应......