簡體   English   中英

如何在JavaFx中為不同的分辨率自動調整窗口大小?

[英]How to automatically resize windows in JavaFx for different resolutions?

我有以下問題:

我在桌面上用full hd創建了一個JavaFX窗口,我設置了這樣的場景:

Scene scene = new Scene(root,1475,1015);

當我在1360*760分辨率的筆記本電腦上運行應用程序時,我看不到整個應用程序,我無法調整大小。

如何設置我的應用程序以自動調整桌面/筆記本電腦的功能及其分辨率和尺寸?

我相信你在尋找這個

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();

這將允許您使用設備的屏幕尺寸,並且您需要做的只是調整大小,使程序中對象的長度/寬度與屏幕的寬度和高度成比例。

提到:

  1. 你必須在JavaFX布局中使用build(BorderPane,GridPane ......等......)

  2. 它不能自動完成。您必須對其進行編程才能完成。

  3. 例如,您通常不知道沒有任務欄的屏幕(寬度或高度)(在Windows,Linux,Mac,Solaris中)。 在這種情況下,你玩getVisualBounds()...

主旋律


你問的是響應式設計 .Below是你想要做的一個例子。雖然不是最好的解決方案,但我的意思是它可以被修改以獲得更好的性能(我還添加了一些代碼來移動窗口,如果它是StageStyle.UNDECORATED拖動窗口看到這個):

在此輸入圖像描述

import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class FX extends Application {

    int screenWidth = (int) Screen.getPrimary().getBounds().getWidth();
    int screenHeight = (int) Screen.getPrimary().getBounds().getHeight();

    Stage stage;
    Scene scene;

    int initialX;
    int initialY;

    @Override
    public void start(Stage s) throws Exception {

        // root
        BorderPane root = new BorderPane();
        root.setStyle("-fx-background-color:rgb(186,153,122,0.7); -fx-background-radius:30;");

        // Responsive Design
        int sceneWidth = 0;
        int sceneHeight = 0;
        if (screenWidth <= 800 && screenHeight <= 600) {
            sceneWidth = 600;
            sceneHeight = 350;
        } else if (screenWidth <= 1280 && screenHeight <= 768) {
            sceneWidth = 800;
            sceneHeight = 450;
        } else if (screenWidth <= 1920 && screenHeight <= 1080) {
            sceneWidth = 1000;
            sceneHeight = 650;
        }

        // Scene
        stage = new Stage();
        stage.initStyle(StageStyle.TRANSPARENT);
        scene = new Scene(root, sceneWidth, sceneHeight, Color.TRANSPARENT);

        // Moving
        scene.setOnMousePressed(m -> {
            if (m.getButton() == MouseButton.PRIMARY) {
                scene.setCursor(Cursor.MOVE);
                initialX = (int) (stage.getX() - m.getScreenX());
                initialY = (int) (stage.getY() - m.getScreenY());
            }
        });

        scene.setOnMouseDragged(m -> {
            if (m.getButton() == MouseButton.PRIMARY) {
                stage.setX(m.getScreenX() + initialX);
                stage.setY(m.getScreenY() + initialY);
            }
        });

        scene.setOnMouseReleased(m -> {
            scene.setCursor(Cursor.DEFAULT);
        });

        stage.setScene(scene);
        stage.show();
    }

    /**
     * Main Method
     * 
     * @param args
     */
    public static void main(String[] args) {
        launch(args);
    }

}

你可以這樣做:

   primaryStage.setMaximized(true);
Scene scene = new Scene(root, Double.MAX_VALUE, Double.MAX_VALUE);

1 - 使用JavaFX布局(BorderPane,GridPane ....等...)

2 - 添加一些布局約束

您可以使用場景構建器生成FXML文件並調整屏幕大小以查看正在發生的事情,請參閱本教程中的布局

這就是你告訴程序根據約束繪制組件的方式,這樣,它將是響應式設計,它將遵循你的約束並自動調整組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM