JavaFX鼠标透明无法正常工作

时间:2015-03-13 22:35:22

标签: java javafx

我试图了解鼠标透明属性。 这是一个示例代码,由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();
    }
}

你能帮助我吗?

2 个答案:

答案 0 :(得分:2)

在这里,一个带有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();
    }
}

答案 1 :(得分:1)

如果您将节点的鼠标透明度设置为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);
    }

}

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

enter image description here

但是,如果您的用例是按钮,则应该启用/禁用按钮。