【发布时间】:2020-03-26 13:44:07
【问题描述】:
每当我切换场景时,UI 都会以这个奇怪的角度裁剪。它发生在我的实际项目中,但有太多的组件要经过,所以我决定做一个虚拟的。实际项目 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);
}`
sample.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>
scene2.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);
}
}
Scene2.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);
}
}
【问题讨论】:
标签: java javafx scenebuilder javafx-11