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