父母舞台上的中心舞台 [英] Center stage on parent stage

查看:99
本文介绍了父母舞台上的中心舞台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JavaFx中创建一个应用程序,如果有任何子阶段被打开,我想要这样做,然后它应该在父阶段的中心打开。
我试图用 mystage.centerOnScreen()来做这件事,但它会将子阶段分配到屏幕的中心,而不是父阶段的中心。
如何将子阶段分配到父阶段的中心?

I am creating an application in JavaFx, In which I want to do that if any child stage is getting opened then it should be opened in center of parent stage. I am trying to do this using mystage.centerOnScreen() but it'll assign the child stage to center of screen, not the center of parent stage. How can I assign the child stage to center of parent stage?

private void show(Stage parentStage) {
    mystage.initOwner(parentStage);
    mystage.initModality(Modality.WINDOW_MODAL);
    mystage.centerOnScreen();
    mystage.initStyle(StageStyle.UTILITY);
    mystage.show();
 }


推荐答案

你可以使用父级的X / Y /宽度/高度属性来做到这一点。您可以执行以下操作:

You can use the parent stage's X/Y/width/height properties to do that. Rather than using Stage#centerOnScreen, you could do the following:

public class CenterStage extends Application {
    @Override
    public void start(final Stage stage) throws Exception {
        stage.setX(300);
        stage.setWidth(800);
        stage.setHeight(400);
        stage.show();

        final Stage childStage = new Stage();
        childStage.setWidth(200);
        childStage.setHeight(200);
        childStage.setX(stage.getX() + stage.getWidth() / 2 - childStage.getWidth() / 2);
        childStage.setY(stage.getY() + stage.getHeight() / 2 - childStage.getHeight() / 2);
        childStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

这篇关于父母舞台上的中心舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆