Java Fx将场景调整为舞台 [英] Java Fx resizing scene to stage
问题描述
我搜索过但找不到与Fx有关的问题。我正在使用Java Fx / JDK 8并且在调整场景大小时遇到问题。下面的代码在场景图中一次只加载一个屏幕,并在屏幕之间切换。问题是当我调整舞台大小时。场景没有调整舞台的大小。 FXML文件中没有什么特别的东西只有锚窗格和一些组件。提前谢谢。
I searched but could not find anything close to the problem that I am having with Fx. I am using Java Fx/JDK 8 and having problems with resizing the scene(s). The below code below loads only one screen at a time in scene graph, and switches between the screens. The problem is when I resize the stage. The scene is not being resized with the stage. Nothing fancy in FXML files only anchor pane and a few components.Thanks in advance.
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
或它的子项设置锚点。这样 mainContainer
只需调整大小为其首选大小,并将子项调整为其首选大小。 prefHeight()
不会指定首选高度,但会计算它。
You don't set the anchors for mainContainer
or it's children. This way mainContainer
simply is resized to it's preferred size and the children are resized to their preferred sizes. prefHeight()
does not assign the preferred height, but calculates it.
虽然不需要指定首选大小,如果你使用锚属性:
Assigning preferred sizes is not necessary though, if you use the anchor properties:
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
s而不是 AnchorPane
s(作为场景根和 ScreensController
的超类型)会更简单,因为它调整大小孩子们无需指定锚点。
Using StackPane
s instead of AnchorPane
s (both as scene root and as supertype of ScreensController
) would be much simpler though, since it resizes the children without the need to specify anchors.
这篇关于Java Fx将场景调整为舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!