JavaFX:如何临时阻止GUI

db9*_*b92 5 java javafx netbeans-platform

我想弄清楚是否可以阻止GUI.基本上,我的应用程序(使用NetBeans平台JavaFX)与服务器有连接.

无论用户看到哪个屏幕,如果应用程序丢失了与服务器的连接,我想阻止所有内容(用户无法打开任何新窗口或点击任何地方),直到应用程序再次连接为止(无论是否重要)需要5分钟或5个小时).尽管如此,最重要的是应该出现一条警告信息(始终在顶部).

正在侦听服务器连接的java类没有对JavaFX容器的任何引用.这就是我实际拥有的:

 public class StatusConnectionObserver implements ConnectionObserver {

        private final Led led;

        private final Label label;

        public StatusConnectionObserver(Led led, Label label) {
            this.led = led;
            this.label = label;
        }

        @Override
        public void setConnected(boolean connected) {
            if (connected) {
                Platform.runLater(() -> {
                    led.setLedColor(Color.rgb(59, 249, 53));
                    label.setText("Connected");
                });

            } else {
                Platform.runLater(() -> {
                    led.setLedColor(Color.RED);
                    label.setText("Disconnected");  
                });                      
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

和:

public class ConnectionComponent {

private Led led;

private Label label;

private HBox container;

private VBox ledContainer;

public ConnectionComponent() {
    initGraphics();
}

public Parent getView() {
    return this.container;
}

public void initGraphics() {
    //Here I set up the elements (label and Led) inside the container
}
Run Code Online (Sandbox Code Playgroud)

这里叫做:

    @ServiceProvider(service = StatusLineElementProvider.class)
public class ConnectionIndicator implements StatusLineElementProvider {

    @Override
    public Component getStatusLineElement() {
        JFXPanel fxPanel = new JFXPanel();
        Platform.setImplicitExit(false);
        new JavaFXUIThread().runOnUiToolkitThread(() -> {
            Scene scene = new Scene(new ConnectionComponent().getView());
            scene.getStylesheets().add(FXTheme.getDefault().getStylesheet());
            fxPanel.setScene(scene);
        });

        return fxPanel;
    }
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是在顶部显示一些内容(即使是简单的文本消息),同时使应用程序在后台更加不透明.

小智 1

你需要一个模态 Dialog。创建这样一个对话框并在连接断开时显示它。然后使用一个线程定期检查您的连接是否已备份。连接激活时终止对话框。由于对话框是模态的,这意味着在解决该对话框之前您无法对 UI 执行任何操作。看到这个