我正在尝试确定是否可以阻止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;
}
}
该想法是在顶部显示某些内容(即使只是简单的文本消息),同时将后台应用程序设置为更加不透明。
label
不是JavaFX UI的一部分(所以我想知道为什么你要将这些调用包装在Platform.runLater(...)
中)。这段代码属于哪个类?如果它没有访问JavaFX UI的权限,那么JavaFX UI是否有访问它的权限?肯定有某种联系。 - James_DStatusConnectionObserver
并注册它,以便它观察某种Connection
对象。为什么不创建另一个具有访问UI权限的ConnectionObserver
实现,以便它可以执行其他答案中描述的所有操作,并向相同的连接对象注册一个该实例?观察者模式的整个重点在于多个实体可以观察相同的数据。 - James_D