2017-01-09 142 views
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; 

,并宣布,一旦初始化,:

回答

1

只是删除的​​3210目前的声明和初始化。这是它是有效最终,可以在一个lambda表达式中使用:

boolean controllerExisted = chatAgentsMap.containsKey(agentKey) ; 

ChatAgentsController chatController = chatAgentsMap.computeIfAbsent(agentKey, k -> { 
    LOGGER.message(String.format("Creating chat window for agent '%d'.", k)); 
    return ChatAgentsController.create(this, k, connectedToOpenfireServer, true); 
}); 

if (controllerExisted) { 
    // 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 { 

    // 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 

    toastScene.setOnMouseClicked(e -> chatController.getChatStage().show()); 

    // chatAgentsMap.put(agentKey, chatController); 
} 
+0

这里: ChatAgentsController chatController = chatAgentsMap.computeIfAbsent(agentKey中,k - > { LOGGER.message(的String.format(“创建聊天窗口代理'%d'。“,k)); return ChatAgentsController.create(this,k,connectedToOpenfireServer,true); }); 它要求尝试抓住,但是当我这样做时,它仍然显示一些错误。其中之一就是lambda表达式中的错误返回类型。 –

+0

所以'ChatAgentsController.create(...)'声明一个检查的异常?在'try'中包装'return'语句并使相应的'catch'块记录异常并返回'null'应该可以工作(尽管不一定是最好的)。 –

+0

它非常完美,非常感谢!你会介意评价这个问题吗?毕竟,我还没有看到过很多这样的问题,而且我知道很多其他人可能会觉得它很有用。 –