我进行了搜索,但找不到与Fx有关的任何问题。我正在使用Java Fx / JDK 8,但无法调整场景大小。下面的以下代码在场景图中一次仅加载一个屏幕,并在屏幕之间切换。问题是当我调整舞台大小时。场景未随舞台调整大小。 FXML文件中没有花哨的东西只是锚窗格和一些组件。在此先感谢。
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;
}
}
}
答案 0 :(得分:1)
您没有为mainContainer
或它的孩子设置锚。这样,mainContainer
的大小就简单地调整为首选大小,子级也被调整为首选大小。 prefHeight()
不会分配首选高度,但会进行计算。
如果您使用锚点属性,则不必分配首选尺寸:
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
的超类型)会更简单,因为它无需指定锚点即可调整子项的大小。