示意

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

 

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

 

相关文章:

  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案