现在我所拥有的解决方案是有一个队列,当有什么东西通过套接字进入队列时,根据队列的内容调用stage.hide()或stage.show()。但是我在实施方面遇到了问题。我有这样的东西:
private BlockingQueue<String> requests;
private Stage primaryStage;
public GraficInterface() {
requests = new LinkedBlockingQueue<String>();
}
@Override
public void start(Stage sp) {
try {
this.primaryStage = sp;
Parent root = FXMLLoader.load(getClass().getResource(...));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
Thread stopper = new Thread(new Runnable() {
public void run() {
String message;
while (true) {
try {
message = requests.take();
if (message.equalsIgnoreCase("show")) {
primaryStage.show();
} else if (message.equalsIgnoreCase("hide")) {
primaryStage.hide();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
stopper.start();
launch();
}
public void addToGraficThread(String string) {
try {
this.requests.put(string);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
队列工作正常,但是当我执行java.lang.NullPointerException
或primaryStage.show()
时我得到primaryStage.hide()
我正在尝试做些什么?还有另一种方法吗?