0
我在解決這個問題時遇到了困難,我無法找到正確的答案來解決我遇到的問題。我希望一個場景/舞臺被用作按鈕來打開另一個場景/舞臺,並且這兩個都是用同樣的方法調用的。下面是完整的方法:使用場景/舞臺作爲按鈕來打開另一個場景/舞臺
public void createChatWindowMinimized(int agentKey, String message)
{
LOGGER.enterMethod();
try{
ChatAgentsController chatController = null;
User agent = getModel().getRealtimeAgentNode().getUser(agentKey);
String firstNameLastName = agent.getFirstname() + " " + agent.getLastName();
UserAvatar userAvatar = getModel().getRealtimeAgentNode().getUserAvatar(agentKey);
ImageView agentAvatar;
agentAvatar = new ImageView();
Stage toastStage = new Stage();
toastStage.initStyle(StageStyle.UNDECORATED);
toastStage.setHeight(120);
toastStage.setWidth(250);
BorderPane toastBorderPane = new BorderPane();
Scene toastScene = new Scene(toastBorderPane);
toastScene.getStylesheets().add(this.getClass().getResource("MessageToaster.css").toExternalForm());
if(userAvatar == null || userAvatar.getBuffer() == null)
agentAvatar.getStyleClass().add("AgentDefaultAvatar");
else
agentAvatar.setImage(new Image(new ByteArrayInputStream(userAvatar.getBuffer()), 48, 48, true, true));
VBox vboxToastImage = new VBox();
vboxToastImage.getStyleClass().add("ToasterImage");
ImageView imgToastAgent = agentAvatar;
imgToastAgent.setFitHeight(60);
imgToastAgent.setFitWidth(60);
vboxToastImage.getChildren().add(imgToastAgent);
toastBorderPane.setLeft(vboxToastImage);
VBox vboxInCenter = new VBox();
Label userName = new Label(firstNameLastName);
userName.getStyleClass().add("ToasterUserName");
Text toasterContent = new Text(message);
toasterContent.getStyleClass().add("ToasterMessage");
vboxInCenter.getChildren().addAll(userName, toasterContent);
vboxInCenter.getStyleClass().add("ToasterBox");
toastBorderPane.setCenter(vboxInCenter);
toastBorderPane.getStyleClass().add("ToasterBorderPane");
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
toastStage.setX(primaryScreenBounds.getMinX() + primaryScreenBounds.getWidth() - TOASTER_WINDOW_WIDTH);
toastStage.setY(primaryScreenBounds.getMinY() + primaryScreenBounds.getHeight() - TOASTER_WINDOW_HEIGHT);
toastStage.setScene(toastScene);
// check if a chat window for this user already exists
if (chatAgentsMap.containsKey(agentKey))
{
// chat window has been created
chatController = chatAgentsMap.get(agentKey);
LOGGER.message(String.format("Showing chat window for agent '%d'.", agentKey));
if(!chatController.getChatStage().isFocused()){
toastStage.show();
chatController.getChatStage().show();
}
}
else
{
// create new chat window
LOGGER.message(String.format("Creating chat window for agent '%d'.", agentKey));
chatController = ChatAgentsController.create(this, agentKey, connectedToOpenfireServer, true);
toastStage.show();
toastStage.setAlwaysOnTop(true);
//-----Here is where I want that the toastStage or the toastScene is clickable by mouse and that it opens the window already created chatController
chatController.getChatStage().show();
chatAgentsMap.put(agentKey, chatController);
}
if (chatController != null && message != null) // append received message to the chat text area
chatController.addChatEntry(agentKey, message);
}
catch (Throwable t)
{
LOGGER.error(t);
}
finally
{
LOGGER.leaveMethod();
}
}
我一直試圖用從點擊鼠標事件,但我不工作,因爲我的變量,實例化其他窗口(chatController.getChatStage()顯示()。)不最後。第一if
語句之前
// ChatAgentsController chatController = null;
,並宣佈,一旦初始化,:
這裏: ChatAgentsController chatController = chatAgentsMap.computeIfAbsent(agentKey中,k - > { LOGGER.message(的String.format(「創建聊天窗口代理'%d'。「,k)); return ChatAgentsController.create(this,k,connectedToOpenfireServer,true); }); 它要求嘗試抓住,但是當我這樣做時,它仍然顯示一些錯誤。其中之一就是lambda表達式中的錯誤返回類型。 –
所以'ChatAgentsController.create(...)'聲明一個檢查的異常?在'try'中包裝'return'語句並使相應的'catch'塊記錄異常並返回'null'應該可以工作(儘管不一定是最好的)。 –
它非常完美,非常感謝!你會介意評價這個問題嗎?畢竟,我還沒有看到過很多這樣的問題,而且我知道很多其他人可能會覺得它很有用。 –