001:获取显示屏参数,根据显示屏参数设置主窗体大小,显示位置。
package ch01;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
/************************************************************************************************************
@Copyright 2021
@package ch01
@file HelloFXApp.java
@date 2021/2/13 21:09
@author qiaowei
@version 1.0
@brief Inherit from Application
@history
************************************************************************************************************/
public class HelloFXApp extends Application {
/********************************************************************************************************
@class HelloFXApp
@date 2021/8/23
@author qiaowei
@version 1.0
@brief 程序启动方法,在JavaFX中,main方法不是必须的,可以通过继承Application的类实现start方法启动程序
@param args 传入的参数
*********************************************************************************************************/
public static void main(String[] args) {
// Launch the instance of HelloFXApp
Application.launch(HelloFXApp.class, args);
}
/********************************************************************************************************
@class HelloFXApp
@date 2021/8/23
@author qiaowei
@version 1.0
@brief No-args constructor, the class must have a no-args constructor inheriting from Application
*********************************************************************************************************/
public HelloFXApp() {
}
/********************************************************************************************************
@class HelloFXApp
@date 2021/8/23
@author qiaowei
@version 1.0
@brief JavaFX Application的启动方法,系统在调用init方法后自动调用start方法,start方法在
JavaFX Application Thread运行
@param primaryStage Main window
********************************************************************************************************/
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello JavaFX Application!");
// 获取系统显示屏参数,
Rectangle2D rectangle2D = Screen.getPrimary().getBounds();
double width = rectangle2D.getWidth();
double height = rectangle2D.getHeight();
// Set the stage position and size
primaryStage.setWidth(width / 2);
primaryStage.setHeight(height / 2);
primaryStage.setX(width / 40);
primaryStage.setY(height / 50);
primaryStage.show();
}
}