JavaFX 鼠标透明不起作用

JavaFX mouse transparent is not working

我正在尝试了解鼠标透明度 属性。 这是一个示例代码,由4个按钮组成 我想让所有按钮都可以点击,但我不知道如何...

public class Example extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        StackPane stackPane = new StackPane();

        ObservableList<Node> children = stackPane.getChildren();

        Button button = new Button("I'm not clickable");
        button.setMaxHeight(Double.MAX_VALUE);
        button.setMaxWidth(Double.MAX_VALUE);
        children.add(button);

        VBox vbox = new VBox();
     // vbox.setMouseTransparent(true); If i put this here, nothing work
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(20);
        vbox.setPrefHeight(Double.MAX_VALUE);
        vbox.setPrefWidth(400);

        ObservableList<Node> vChildren = vbox.getChildren();
        vChildren.add(new Button("This"));
        vChildren.add(new Button("Button"));
        vChildren.add(new Button("Are clickable"));

        BorderPane borderPane = new BorderPane();
     // borderPane.setMouseTransparent(true); If i put this here, nothing work
        borderPane.setLeft(vbox);
        children.add(borderPane);

        stage.setScene(new Scene(stackPane, 800, 600));
        stage.show();
    }
}

你能帮帮我吗?

如果将节点的鼠标透明度设置为 false,则它根本不会接收鼠标事件。

我创建了一个小示例来帮助您了解鼠标透明度的作用。

public class MouseTransparency extends Application {

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        Rectangle outerRect = new Rectangle(100,100,200,200);
        outerRect.setStroke(Color.BLUE);
        outerRect.setFill(Color.BLUE.deriveColor(1, 1, 1, 0.2));

        Rectangle innerRect = new Rectangle(150,150,50,50);
        innerRect.setStroke(Color.RED);
        innerRect.setFill(Color.RED.deriveColor(1, 1, 1, 0.2));

        Circle circle = new Circle( 250, 250, 50);
        circle.setStroke(Color.GREEN);
        circle.setFill(Color.GREEN.deriveColor(1, 1, 1, 0.2));

        // mouse transparency checkbox
        CheckBox checkBox = new CheckBox( "Enable Mouse Transparency");

        // bind inner rect mouse transparency to the checkbox value; in the end you'd rather use innerRect.setMouseTransparent(...); 
        innerRect.mouseTransparentProperty().bind(checkBox.selectedProperty());

        Label label = new Label("You clicked: ");

        // add event handlers
        outerRect.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> label.setText("You clicked: Outer Rectangle"));
        innerRect.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> label.setText( "You clicked: Inner Rectangle"));
        circle.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> label.setText( "You clicked: Circle"));

        VBox vBox = new VBox();
        vBox.getChildren().addAll( checkBox, label);

        root.getChildren().addAll( vBox, outerRect, innerRect, circle);

        Scene scene = new Scene( root, 500, 500);

        primaryStage.setScene( scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

如果您在鼠标透明度未激活时单击内部矩形,它将接收事件。如果您通过选中复选框激活内部矩形的鼠标透明度,则当您单击内部矩形时,外部矩形将接收事件。

但是,如果您的预期用例是按钮,则您应该 enable/disable 按钮。

给你,一个大按钮,里面有 3 个按钮。但正如我所说,这根本没有任何意义。不是来自编码,不是来自可用性。但如果你必须,你必须。这是代码:

public class Example extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {

        StackPane stackPane = new StackPane();

        Button bigButton = new Button("I'm not clickable");
        bigButton.setOnAction(e -> System.out.println(e));
        bigButton.setMaxHeight(Double.MAX_VALUE);
        bigButton.setMaxWidth(Double.MAX_VALUE);
        stackPane.getChildren().add(bigButton);

        VBox vbox = new VBox();
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(20);
        vbox.setPrefHeight(Double.MAX_VALUE);
        vbox.setPrefWidth(400);

        ObservableList<Node> vChildren = vbox.getChildren();
        Button button1 = new Button("This");
        button1.setOnAction(e -> {
            System.out.println(e);
            e.consume();
        });
        Button button2 = new Button("Button");
        button2.setOnAction(e -> {
            System.out.println(e);
            e.consume();
        });
        Button button3 = new Button("Are clickable");
        button3.setOnAction(e -> {
            System.out.println(e);
            e.consume();
        });

        vChildren.addAll(button1, button2, button3);

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(vbox);

        vbox.prefWidthProperty().bind(bigButton.widthProperty());
        vbox.prefHeightProperty().bind(bigButton.heightProperty());

        bigButton.setGraphic(borderPane);

        stage.setScene(new Scene(stackPane, 800, 600));
        stage.show();
    }
}