javafx swing 窗体启动后在屏幕中间

示意

 

 

 

  this.setLocation(centerX - halfwidth, centerY - halfHeight);

 

使用

  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

 

来获取显示器的尺寸

然后就可以得到宽高值,就知道了中点位置

 int centerX = screenSize.width / 2;
        int centerY = screenSize.height / 2;

 

然后还要知道窗体的大小然后就知道了窗体的中心点

  Dimension jfSize = this.getSize();
        int halfwidth = jfSize.width / 2;
        int halfHeight = jfSize.height / 2;

 完整代码

 

      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = screenSize.width / 2;
        int centerY = screenSize.height / 2;
        Dimension jfSize = this.getSize();
        int halfwidth = jfSize.width / 2;
        int halfHeight = jfSize.height / 2;
        this.setLocation(centerX - halfwidth, centerY - halfHeight);

 

 

 

posted @ 2020-10-31 15:07  不打鱼光晒网  阅读(363)  评论(0)    收藏  举报