gpt4 book ai didi

Java Fx 将场景大小调整为舞台

转载 作者:行者123 更新时间:2023-12-02 10:35:57 29 4
gpt4 key购买 nike

我进行了搜索,但找不到任何与我在 Fx 中遇到的问题相近的内容。我正在使用 Java Fx/JDK 8,并且在调整场景大小时遇到​​问题。下面的代码在场景图中一次仅加载一个屏幕,并在屏幕之间切换。问题是当我调整舞台大小时。场景没有随舞台调整大小。 FXML 文件中没有什么奇特的东西,只有锚定 Pane 和一些组件。提前致谢。

public class ScreensFramework extends Application {

static String main = "main";
static String mainFile = "Main.fxml";
static String play = "play";
static String playFile = "Play.fxml";


public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage mainStage) {
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(ScreensFramework.main, ScreensFramework.mainFile);
mainContainer.loadScreen(ScreensFramework.play, ScreensFramework.playFile);

mainContainer.setScreen(ScreensFramework.main);

AnchorPane root = new AnchorPane();
root.getChildren().setAll(mainContainer);
Scene scene = new Scene(root);
mainStage.setScene(scene);
//This did not work out either
mainStage.heightProperty().addListener((observable, oldValue, newValue) -> {
root.getScene().prefHeight((double) newValue);
});
mainStage.show();
}
}

public class MainController implements Initializable, ControlledScreen {

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb) {
}

public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
}

public class PlayController implements Initializable, ControlledScreen {

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb) {
}

public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
}


public class ScreensController extends AnchorPane {

private HashMap<String, Node> screens = new HashMap<>();

public void addScreen(String name, Node screen) {
screens.put(name, screen);
}

public boolean loadScreen(String name, String resource) {
try {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

public boolean setScreen(final String name) {

if (screens.get(name) != null) {
if (!getChildren().isEmpty()) {
getChildren().remove(0); //remove the displayed screen
getChildren().add(0, screens.get(name)); //add the screen

} else {
getChildren().add(screens.get(name));
}
return true;
} else {
return false;
}
}
}

最佳答案

您没有为 mainContainer 或其子级设置 anchor 。这样,mainContainer 就可以简单地调整到其首选大小,并且子级也可以调整到其首选大小。 prefHeight() 不会分配首选高度,而是计算它。

如果您使用 anchor 属性,则不必指定首选尺寸:

AnchorPane.setTopAnchor(mainContainer, 0d);
AnchorPane.setRightAnchor(mainContainer, 0d);
AnchorPane.setBottomAnchor(mainContainer, 0d);
AnchorPane.setLeftAnchor(mainContainer, 0d);
public void addScreen(String name, Node screen) {
screens.put(name, screen);
AnchorPane.setTopAnchor(screen, 0d);
AnchorPane.setRightAnchor(screen, 0d);
AnchorPane.setBottomAnchor(screen, 0d);
AnchorPane.setLeftAnchor(screen, 0d);
}

使用 StackPane 代替 AnchorPane(既作为场景根又作为 ScreensController 的父类(super class)型)会简单得多,因为它无需指定 anchor 即可调整子级的大小。

关于Java Fx 将场景大小调整为舞台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293294/

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