Java 将场景大小调整到舞台
Java Fx resizing scene to stage
我进行了搜索,但找不到与我在 Fx 中遇到的问题相近的任何内容。我正在使用 Java Fx/JDK 8 并且在调整场景大小时遇到问题。下面的代码在场景图中一次只加载一个屏幕,并在屏幕之间切换。问题是当我调整舞台大小时。场景未随舞台调整大小。 FXML 文件中没什么特别的,只有锚定窗格和一些 components.Thanks 提前。
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
或 children 设置锚点。这样 mainContainer
就可以简单地调整到它的首选大小,而 children 也可以调整到它们的首选大小。 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
s 而不是 AnchorPane
s(作为场景根和 ScreensController
的超类型)会简单得多,因为它会调整 children 的大小无需指定锚点。
我进行了搜索,但找不到与我在 Fx 中遇到的问题相近的任何内容。我正在使用 Java Fx/JDK 8 并且在调整场景大小时遇到问题。下面的代码在场景图中一次只加载一个屏幕,并在屏幕之间切换。问题是当我调整舞台大小时。场景未随舞台调整大小。 FXML 文件中没什么特别的,只有锚定窗格和一些 components.Thanks 提前。
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
或 children 设置锚点。这样 mainContainer
就可以简单地调整到它的首选大小,而 children 也可以调整到它们的首选大小。 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
s 而不是 AnchorPane
s(作为场景根和 ScreensController
的超类型)会简单得多,因为它会调整 children 的大小无需指定锚点。