gpt4 book ai didi

java - 尝试在我的 Controller 中使用原始版本和全屏版本的 Pane 也会更改窗口版本中的 Pane

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

我有一个 ScreenController 类,可以在 JavaFX 程序中轻松切换根 Pane 。它使用 HashMap 来完成此操作,该 HashMap 用 String 键存储 Pane 值。

我尝试对其进行设置,以便每当将新 Pane 添加到 SceneController 对象时,它都会创建给定 Pane 的全屏版本,并将其添加到与第一个具有相同字符串键的第二个 HashMap 中。

但是,现在每当我切换屏幕时,总是使用全屏版本的 Pane 。

似乎我用来创建全屏 Pane 版本的 goFullscreen() 方法正在修改两者,即使 Java 是按值传递的。

如何获取 ScreenController 类 windowedRootMap 中的 HashMap 属性,返回原始 Pane 而不进行全屏缩放?

/**
* Creates an appropriately scaled fullscreen version of the argument Pane object.
*
* @param contentPane The Pane to create a fullscreen version of.
* @return Pane
*/
private static Pane goFullscreen(Pane contentPane) {

// get original dimensions and their ratio.
final double windowedWidth = 1280.0;
final double windowedHeight = 800.0;
final double windowedRatio = windowedWidth / windowedHeight;

// get fullscreen width and height from monitor dimensions
final double fullscreenWidth = Screen.getPrimary().getBounds().getWidth();
final double fullscreenHeight = Screen.getPrimary().getBounds().getHeight();

// find how much to scale by
double scaleFactor;
if (fullscreenWidth / fullscreenHeight > windowedRatio)
scaleFactor = fullscreenHeight / windowedHeight;
else
scaleFactor = fullscreenWidth / windowedWidth;

// scale the contents of the Pane appropriately
Scale scale = new Scale(scaleFactor, scaleFactor);
contentPane.getTransforms().setAll(scale);
contentPane.setPrefWidth(fullscreenWidth / scaleFactor);
contentPane.setPrefWidth(fullscreenHeight / scaleFactor);

return contentPane;
}

/**
* Allows switching root nodes easily.
*/
private class ScreenController {

private HashMap<String, Pane> windowedRootMap = new HashMap<>();
private HashMap<String, Pane> fullscreenRootMap = new HashMap<>();
private Scene currentScene;

private ScreenController(Scene currentScene) {

this.currentScene = currentScene;
}

private void addScreen(String name, Pane pane) {

this.windowedRootMap.put(name, pane);
this.fullscreenRootMap.put(name, goFullscreen(pane));
}

private void activate(String name) {

this.currentScene.setRoot(this.windowedRootMap.get(name));
}
}

最佳答案

您认为 Java 是按值传递的,这是正确的,但关于传递的“值”的内容是错误的。在 Java 中,它是对被复制对象的引用,但新引用仍然指向同一个对象实例。以下问题的许多精彩答案对此进行了更详细的解释:

假设您不想维护两个不同的 Pane 实例,一种选择是根据您所处的模式简单地设置属性。我不太确定您想要什么代码最终看起来像这样,但它可能看起来像这样:

import java.util.Map;
import java.util.HashMap;
import javafx.geometry.Dimension2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;

public class ScreenController {

private final Map<String, Foo> map = new HashMap<>();
private final Scene scene;

public ScreenController(Scene scene) {
this.scene = scene;
}

public void register(String name, Pane pane) {
map.put(name, pane);
}

public void activate(String name) {
scene.setRoot(map.get(name).pane);
}

private static class Foo {

private final Pane pane;
private final Dimension2D normalSize;
private final Dimension2D fullScreenSize;
private final Scale scale;

private Foo(Pane pane) {
this.pane = pane;
// set the other fields...
}

private void enterFullScreenMode() {
pane.setPrefSize(fullScreenSize.getWidth(), fullScreenSize.getHeight());
pane.getTransforms().add(scale);
}

private void exitFullScreenMode() {
pane.setPrefSize(normalSize.getWidth(), normalSize.getHeight());
pane.getTransforms().remove(scale);
}

}

}

注意:我没有包含任何参数或状态验证。

但是请记住,场景的根的大小与场景图中的“常规”节点不同。设置首选大小可能不会产生任何效果。来自 documentation .

The application must specify the root Node for the scene graph by setting the root property. If a Group is used as the root, the contents of the scene graph will be clipped by the scene's width and height and changes to the scene's size (if user resizes the stage) will not alter the layout of the scene graph. If a resizable node (layout Region or Control) is set as the root, then the root's size will track the scene's size, causing the contents to be relayed out as necessary.

<小时/>

如果您只有一定数量的屏幕,请考虑使用enum作为Map的键,而不是String。另请查看 java.util.EnumMap .

关于java - 尝试在我的 Controller 中使用原始版本和全屏版本的 Pane 也会更改窗口版本中的 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570939/

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