Java 使用场景/舞台作为按钮打开另一个场景/舞台

Java 使用场景/舞台作为按钮打开另一个场景/舞台,java,javafx,Java,Javafx,我在解决这个问题上遇到了困难,我找不到一个正确的答案来解决这个问题。我希望将一个场景/舞台用作打开另一个场景/舞台的按钮,并使用相同的方法调用这两个场景/舞台。以下是完整的方法: public void createChatWindowMinimized(int agentKey, String message) { LOGGER.enterMethod(); try{ C

我在解决这个问题上遇到了困难,我找不到一个正确的答案来解决这个问题。我希望将一个场景/舞台用作打开另一个场景/舞台的按钮,并使用相同的方法调用这两个场景/舞台。以下是完整的方法:

 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();
        }
    }

我一直在尝试使用CLICK中的鼠标事件,但没有成功,因为实例化另一个窗口的变量(chatController.getChatStage().show())不是最终变量。

只需删除当前的声明和chatController的初始化即可:

// ChatAgentsController chatController = null;
在第一个
if
语句之前声明并初始化它一次。也就是说,它实际上是最终的,可以在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);
}

此处:ChatAgentsController chatController=chatAgentsMap.computeIfAbsent(agentKey,k->{LOGGER.message(String.format(“为代理“%d.”创建聊天窗口”),返回ChatAgentsController.create(this,k,connectedToOpenfireServer,true);});它要求放置一个try-catch,但当我这样做时,它仍然显示出一些错误。其中之一就是lambda表达式中的错误返回类型。那么
ChatAgentsController.create(…)
声明了一个选中的异常?将
return
语句包装在
try
中,并使相应的
catch
块记录异常和return
null
应该可以工作(虽然不一定是最好的)。它工作得很好,非常感谢!你介意给这个问题打分吗?毕竟,我还没有见过很多这样的问题,我知道很多其他人可能会觉得它很有用。