有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javajavafx:如何临时阻止GUI

我试图弄清楚是否有可能阻止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");  
                });                      
            }
        }
}

以及:

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
}

这里叫做:

    @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;
    }
}

这样做的目的是在顶部显示一些内容(甚至是一条简单的短信),同时使应用程序在后台更加不透明


共 (2) 个答案

  1. # 1 楼答案

    使用AlertDialog组件。您可以通过CSS或添加自定义内容来设置样式。尝试以下最简单的解决方案:

    Alert a = new Alert(Alert.AlertType.ERROR, "Connection error");
    
    public void createAlert() {
        a.getDialogPane().getButtonTypes().clear();
        a.initModality(Modality.APPLICATION_MODAL);
        //*************** EDIT ***************
        a.initStyle(StageStyle.UNDECORATED);
        a.initOwner(label.getScene().getWindow());
        //************************************
    }
    
    @Override
    public void setConnected(boolean connected) {
        if (connected) {
            Platform.runLater(() -> {
                label.setText("Connected");
                a.show();
            });
    
        } else {
            Platform.runLater(() -> {
                label.setText("Disconnected");
                a.close();
            });
        }
    }
    

    您还可以在整个Scene之上添加额外的Pane

    StackPane root = new StackPane();
    root.getChildren().addAll(applicationContent);
    Pane p = new Pane();
    p.setStyle("-fx-background-color: rgba(31,31,31,0.6);");
    //add Pane to root when disconnected
    //root.getChildren().add(p);
    
    Scene scene = new Scene(root, 300, 250);
    
  2. # 2 楼答案

    您需要一个模式Dialog。创建这样一个对话框,并在连接中断时显示它。然后使用一个线程定期检查连接是否已备份。当连接激活时,终止对话。由于对话框是模态的,这意味着在解决它之前,您不能对UI做任何事情。见this