繁体   English   中英

javafx 8,不使用 fxml,scene.lookup 找不到节点

[英]javafx 8, not using fxml, scene.lookup not finding node

在以下代码中,primaryScene.lookup(第 30 行)返回 null。 但是在调试模式(IntelliJ)中,我可以深入查看场景图中的节点。

这是一个独立的例子。 没有 FXML。

该应用程序仅绘制舞台和场景,然后尝试使用 Scene.lookup 选择标记为 id="drawPane" 的窗格。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import static javafx.application.Application.launch;

public class Main extends Application {
    Scene primaryScene;
    Pane drawPane;

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

    }

    public void start(Stage primaryStage) {
        primaryScene = buildUI(primaryStage);
        if (primaryScene == null) throw new NullPointerException();

        drawPane = (Pane)primaryScene.lookup("#drawPane");
        if (drawPane == null) {
            throw new NullPointerException();
    }

    primaryStage.show();
}

private Scene buildUI(Stage primaryStage) {
    ScrollPane scrollPane;
    ImageView iv;

    BorderPane root = new BorderPane();
    Scene primaryScene = new Scene(root, 900, 800);
    initializePrimaryStage(primaryStage, primaryScene);
    initializeFrameContent(root);
    initializeContent(root);
    return primaryScene;

}

private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
    primaryStage.setTitle("Image Clip Test");
    primaryStage.setScene(primaryScene);
    primaryStage.setWidth(400);
    primaryStage.setHeight(300);
    primaryStage.minWidthProperty().setValue(400);
    primaryStage.minHeightProperty().setValue(300);
}

private void initializeFrameContent(BorderPane root) {
    Button leftButton = new Button("LEFT");
    leftButton.setId("leftButton");
    Button rightButton = new Button("RIGHT");
    rightButton.setId("rightButton");
    Button topButton = new Button("TOP");
    rightButton.setId("topButton");

    StackPane leftPane = new StackPane(leftButton);
    leftPane.setAlignment(Pos.TOP_LEFT);

    StackPane rightPane = new StackPane(rightButton);
    rightPane.setAlignment(Pos.TOP_RIGHT);

    root.setLeft(leftPane);
    root.setTop(topButton);
    root.setRight(rightButton);
}

private void initializeContent(BorderPane root) {
    Pane drawPane = new Pane();
    drawPane.setId("drawPane");
    drawPane.setPrefSize(1000, 800);
    drawPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

    ScrollPane scrollPane = new ScrollPane(drawPane);
    scrollPane.setPrefSize(300, 300);
    scrollPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
    scrollPane.setFitToHeight(true);
    scrollPane.setFitToWidth(true);
    scrollPane.setStyle("-fx-focus-color: transparent");

    root.setCenter(scrollPane);
    }
}

在应用 CSS 之前,查找不起作用,这通常在场景布局并渲染到屏幕之前不会发生。

以下将强制此操作更早发生,以便您可以访问查找:

public void start(Stage primaryStage) {
    primaryScene = buildUI(primaryStage);
    if (primaryScene == null) throw new NullPointerException();

    primaryScene.getRoot().applyCss();

    drawPane = (Pane)primaryScene.lookup("#drawPane");
    if (drawPane == null) {
        throw new NullPointerException();
}

也偶然发现了这种情况。 上述解决方案不起作用,通过检查由 Gluon WYSIWYG 生成的 .fxml 文件,注意到有两个 id 字段。

id="内容" fx:id="工作面板"

所需要做的就是删除id="Content" ,然后另一个 id 就按预期工作了。

奇怪的是,调用new ScrollPane(drawPane)并没有将 drawPane 添加到 ScrollPane 子列表中。 我尝试使用scrollPane.setContent(drawPane) ,但它也不起作用。 这就是为什么您不能通过 id 查找它的原因。 但是您可以在使用以下代码显示窗口后通过其 id 查找drawPane

Platform.runLater(() -> {
  drawPane = (Pane)primaryScene.lookup("#drawPane");
  if (drawPane == null) {
    throw new NullPointerException(); // this will not throw
  }
});

暂无
暂无

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

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