1
我想在某些用户操作后显示NotificationPane。我的应用程序有多个场景,NotificationPane应显示在当前活动的场景中。NotificationPane不会出现在场景
整件事情与通知一起工作,它在我需要它时弹出。 但我不知道如何使这项工作的NotificationPane。
步骤我迄今所取得:
- 我tryed直接把NotificationPane我的现场,并拨打
show()
- 它的工作原理。 - 如今的想法是通过调用
stage.getScene().getRoot()
获得当前窗格,它换到NotificationPane,然后调用show()
- 它不工作,我不知道为什么。 ((BorderPane) pane).setCenter(new Label("TEST"));
此行与文本标签更换按钮,所以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");
}
}
}