java没有舞台,Java Fx将场景调整为舞台

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 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;

}

}

}

解决方案

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);

}

Using StackPanes instead of AnchorPanes (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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值