Java 通知窗格没有';我没有出现在现场

Java 通知窗格没有';我没有出现在现场,java,controlsfx,Java,Controlsfx,我想在某些用户操作之后显示NotificationPane。我的应用程序有多个场景,NotificationPane应该显示在当前活动的场景中 整个过程都与通知一起工作,当我需要它时,它会弹出。 但我不知道如何让NotificationPane工作 到目前为止,我采取了以下步骤: 我试着把NotificationPane直接放到我的场景中,然后打电话 show()-它可以工作 现在的想法是通过调用 stage.getScene().getRoot(),将其包装到NotificationPane

我想在某些用户操作之后显示NotificationPane。我的应用程序有多个场景,NotificationPane应该显示在当前活动的场景中

整个过程都与通知一起工作,当我需要它时,它会弹出。 但我不知道如何让NotificationPane工作

到目前为止,我采取了以下步骤:

  • 我试着把NotificationPane直接放到我的场景中,然后打电话
    show()
    -它可以工作
  • 现在的想法是通过调用
    stage.getScene().getRoot()
    ,将其包装到NotificationPane,然后调用
    show()
    -它不起作用,我不知道为什么
  • ((边框窗格)窗格).setCenter(新标签(“测试”)此行将用文本标签替换按钮,因此
    stage.getScene().getRoot()
    将返回正确的对象
我做了一个简单的程序来测试这种行为。一个按钮调用NotificationPane。 有什么建议吗

这是我的测试程序:

package application;

import org.controlsfx.control.NotificationPane;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        Button notificationPaneButton = new Button("NotificationPane");
        notificationPaneButton.setOnAction(e -> showNotificationPane(primaryStage, "Notification text"));

        VBox vbox = new VBox(5);
        vbox.setAlignment(Pos.CENTER);
        vbox.getChildren().addAll(notificationPaneButton);

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

        primaryStage.setTitle("Notifications test");
        primaryStage.setScene(new Scene(borderPane, 300, 200));
        primaryStage.show();
    }

    public void showNotificationPane(Stage stage, String message) {
        Parent pane = stage.getScene().getRoot();
//      ((BorderPane) pane).setCenter(new Label("TEST"));
        NotificationPane notificationPane = new NotificationPane(pane);
        notificationPane.setText(message);
        if (notificationPane.showingProperty().get()) {
            notificationPane.hide();
            System.err.println("hide");
        } else {
            notificationPane.show();
            System.err.println("show");
        }

    }
}

好的,我现在明白问题了。包装当前窗格是不够的,我还必须将NotificationPane添加到场景中。对吧?

无论如何,我当前的解决方案如下:

  • 获取当前场景
  • 获取当前窗格
  • 包裹窗格
  • 将当前场景替换为新场景
为了避免多次包装
NotificationPane
,我检查当前窗格是否已经是
NotificationPane
,然后调用
show()

public void showNotificationPane(Stage stage) {
    Scene scene = stage.getScene();
    Parent pane = scene.getRoot();
    if (!(pane instanceof NotificationPane)){
        NotificationPane notificationPane = new NotificationPane(pane);
        scene = new Scene(notificationPane, scene.getWidth(), scene.getHeight());
        stage.setScene(scene);
        notificationPane.show();
    } else {
        ((NotificationPane)pane).show();
    }
}