如何在不减少 hitbox 的情况下使用 JavaFX 创建透明 Button?

How to create a transparent Button with JavaFX without decreasing the hitbox?

只要我在 JavaFX 中创建一个新的 Button 并将背景设置为透明:myButton.setBackground(Background.EMPTY);myButton.setStyle("-fx-background-color: transparent;"), 当通过以下方式捕获 ActionEvent 时,点击框将仅包含按钮中的文本:

myButton.setOnAction(newjavafx.event.EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        //handle UI input
    }
});

所以我必须瞄准一个字母并单击它,这很烦人,尤其是在更改文本和/或小文本时。

如何在透明背景下保持碰撞箱不变?

使用

myButton.setPickOnBounds(true);

这意味着按钮的布局边界将用于确定鼠标点击它,而不是它的非透明像素集。