每当我切换场景时,用户界面就会以这个奇怪的角度被裁剪。它发生在我的实际项目中,但有太多组件需要处理,所以我决定制作一个虚拟的。实际项目VBox作为root,这里是AnchorPane。结果完全相同。
预期用户界面:1
更改为场景2:2
切换回场景1:3
Main.java `
public void start(Stage primaryStage) throws Exception{
AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setResizable(true);
primaryStage.show();
primaryStage.setFullScreen(true);
primaryStage.setMaximized(true);
}`
样本.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Button mnemonicParsing="false" onAction="#loadScene2" text="Go to Scene 2" />
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
场景2.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="root" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Scene2">
<children>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Button mnemonicParsing="false" onAction="#loadScene2" text="Go to scene 1" />
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Controller .java:
public class Controller {
@FXML
private AnchorPane root;
@FXML
private void loadScene2() throws IOException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("scene2.fxml"));
root.getChildren().setAll(pane);
}
}
场景2.java:
public class Scene2 {
@FXML
private AnchorPane root;
@FXML
private void loadScene2() throws IOException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
root.getChildren().setAll(pane);
}
}
最佳答案
所以你的问题是,当你发布你正在打电话时,你从来没有真正卸下以前的锚定痛苦
root.getChildren()
这意味着您只是将示例锚定 Pane 的内容设置到嵌套它们的另一个锚定 Pane ,从而导致这种奇怪的行为。
试试这个,看看它是否有效
Parent parent = root.getParent();
parent.getChildren().remove(root);
parent.getChildren().add(pane);
这应该可以解决您的问题,如果可以解决问题请告诉我
关于JAVAFX 舞台会根据场景的变化而改变其大小。或者这就是我的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60868700/