gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:02:37 25 4
gpt4 key购买 nike

我在解决这个问题时遇到了困难,并且我找不到我遇到的这个问题的正确答案。我希望将一个场景/阶段用作打开另一个场景/阶段的按钮,并且两者都通过相同的方法调用。完整的方法如下:

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

关于java - 使用场景/舞台作为按钮打开另一个场景/舞台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41553435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com