简体   繁体   English

JavaFX:如何使底层节点可访问?

[英]JavaFX: How to make the underlaying Node accessable?

I have a Pane and a VBox inside of a StackPane . 我在StackPane有一个Pane和一个VBox I added the Pane first and the VBox on top of it. 我首先添加了Pane并在其上添加了VBox The VBox includes several kes that in turn have Button s as children. VBox包括几个kes,而这些kes又将Button作为孩子。 I use the normal Pane as a "canvas" to position Line s on it. 我使用普通的Pane作为“画布”来定位Line s。 The Line s as well as the Button s need to be interactive. LineButton需要是交互式的。 So by clicking on them they shall for example change their color. 因此,通过点击它们,它们将例如改变它们的颜色。 But at the moment the Pane and its Line objects are shown but they are covered by the VBox so I can not interact with them but only with the Button s. 但是现在显示了Pane及其Line对象,但它们被VBox覆盖,因此我无法与它们交互,只能与Button交互。

How can I provide that I can interact with the Line as well, though they are in a lower layer of the StackPane ? 我怎么能提供我也可以与Line交互,虽然它们位于StackPane的较低层?

They are covered cause the VBOX is in front of the Pane. 它们被覆盖因为VBOX在Pane前面。

First Way: 第一种方式:

You can setVisible(false) the VBox so the Pane can be accessible and then setVisible(true) the VBox again. 您可以setVisible(false) VBox,以便可以访问窗格,然后再次设置VBox可见setVisible(true)

Second Way: 第二种方式:

You can use methods called toBack(); 您可以使用名为toBack();方法toBack(); and toFront(); toFront(); and bring a Node back or front to the hierarchy: 并将Node返回或前置到层次结构中:

vBox.toBack(); //the the vBox goes back to the hierarchy,it is like zOrder in html

and then use: 然后使用:

 vBox.toFront(); //to bring the vBox again in front.

Finally: 最后:

You can somewhere provide a ToggleButton that when is pressed the VBox is appearing and when is not pressed the VBox is disappeared. 你可以在某处提供一个ToggleButton ,当按下时, VBox出现,当没有按下时, VBox就消失了。

You can set the pickOnBoundsProperty of your container Node s ( VBox and HBox ) to false . 您可以将容器NodeVBoxHBox )的pickOnBoundsProperty设置为false

Defines how the picking computation is done for this node when triggered by a MouseEvent or a contains function call. 定义在MouseEvent或contains函数调用触发时如何为此节点执行拾取计算。 If pickOnBounds is true, then picking is computed by intersecting with the bounds of this node, else picking is computed by intersecting with the geometric shape of this node. 如果pickOnBounds为true,则通过与此节点的边界相交来计算拾取,否则通过与此节点的几何形状相交来计算拾取。

As result, "transparent" areas of the HBox and the VBox will not register the click event. 因此, HBoxVBox “透明”区域将不会注册click事件。

Example: 例:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            Pane pane = new Pane();
            pane.setStyle("-fx-background-color:red;");
            StackPane sp = new StackPane();
            VBox vbox = new VBox();

            HBox hbox = new HBox();
            hbox.setSpacing(30);


            for (int i = 0; i< 5; i++) {
                Button b = new Button("Button");
                b.setOnAction(e -> System.out.println("Button clicked"));
                hbox.getChildren().add(b);
            }

            vbox.getChildren().add(hbox);
            sp.getChildren().addAll(pane, vbox);

            Line line = new Line(10, 10, 500, 10);
            line.setStrokeWidth(3);
            pane.getChildren().add(line);
            line.setOnMouseClicked(e -> {
                System.out.println("Line Clicked!");
            });

            // Set pickOnBounds to vbox and hbox
            vbox.setPickOnBounds(false);
            hbox.setPickOnBounds(false);
            root.setCenter(sp);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM