工具栏未隐藏在粒子应用程序上

Toolbar is not hidding on ParticleApplication

我有以下代码:

@ParticleView(isDefault=true, name="login")
public class LoginView extends FXMLView {

   public LoginView() {
       super(LoginView.class.getResource("login.fxml"));
   }

   @Override
   public void start() {
       ((LoginController) getController()).postInit();
   }

   @Override
   public void stop() {
        ((LoginController) getController()).dispose();
 }

}

控制器相关代码为:

public class LoginController {
  @Inject
  ParticleApplication app;

  @Inject
  private ViewManager viewManager;

  @Inject
  private StateManager stateManager;

  @Inject
  private MenuBar menuBar;
  @Inject
  private ToolBar toolBar;
  @Inject
  private StatusBar statusBar;

  @FXML
  private TextField txfUsuario;
  @FXML
  private PasswordField txfPassword;

  public void initialize() {
    ActionMap.register(this);
  }

  public void postInit() {
      app.setShowCloseConfirmation(false);
      toolBar.setVisible(false);
      menuBar.setVisible(false);
  }
}

菜单栏不可见(但 space 仍然存在)但工具栏仍然可见。

有什么建议吗?

如果您 运行 使用常规 JavaFX 应用程序进行此简短测试,您会注意到相同的行为:

@Override
public void start(Stage primaryStage) {
    ToolBar toolBar = new ToolBar(new Button("Click"));
    StackPane pane = new StackPane(new Label("A label"));
    pane.setStyle("-fx-background-color: yellow");
    BorderPane root = new BorderPane(pane);
    root.setTop(toolBar);

    Scene scene = new Scene(root, 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();

    toolBar.setVisible(false);
}

根据节点的 JavaDoc managed 属性:

Defines whether or not this node's layout will be managed by it's parent. If the node is managed, it's parent will factor the node's geometry into its own preferred size and layoutBounds calculations and will lay it out during the scene's layout pass. If a managed node's layoutBounds changes, it will automatically trigger relayout up the scene-graph to the nearest layout root (which is typically the scene's root node).

这意味着边框面板的顶部区域保持工具栏(默认情况下已管理)给定的首选大小,无论其可见性如何。

当您将 ToolBar 的可见性设置为 false 时,您还需要通过将 managed 设置为 false 来释放其 space:

@Override
public void start(Stage primaryStage) {
    ...       
    toolBar.setVisible(false);
    toolBar.setManaged(false);
}

编辑

在你的情况下,你还有一个MenuBar,而ToolBar managed 属性 已经绑定了它的可见性,所以你只需要设置:

public void postInit() {
    app.setShowCloseConfirmation(false);
    Platform.runLater(() -> {
        toolBar.setVisible(false);

        menuBar.setVisible(false);
        menuBar.setManaged(false);
    });
}

注意:我添加了Platform.runLater()postInit()方法在舞台显示之前被调用,所以这是延迟它的一种方式位并让控件在处理它们的可见性之前正确呈现。

如果您想恢复他们的可见性,这也应该有效:

public void setTopVisible() {
    toolBar.setVisible(true);

    menuBar.setVisible(true);
    menuBar.setManaged(true;
}