How to automatically resize windows in JavaFx for different resolutions?

时光毁灭记忆、已成空白 提交于 2020-01-22 20:14:49

问题


I have the following problem:

I have created a JavaFX window on a desktop with full hd, and I set the scene like this:

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

When I run the application on a laptop with 1360*760 resolution, I can't see the whole application and I can't resize it.

How can I set my application to resize automatically in function of the desktop/laptop and it`s resolution and dimensions?


回答1:


I believe you are looking for this

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

This will allow you to use the screen size of your device and all you need to do to resize is make the length/width of the objects within your program proportional to the width and height of the screen.




回答2:


Mention that:

  1. You have to use build in JavaFX layouts (BorderPane,GridPane.... etc...)

  2. It cannot be done automatically.You have to program it to do so.

  3. It is common for example that you want to know the screen(width or height) without the taskbar (in Windows,Linux,Mac,Solaris). In that case you play with getVisualBounds()...

Main theme


You are asking about Responsive Design.Below is an example of what you want to make.Although is not best solution,with this i mean it can be modified for better performance(I also have added some code to move the window if it is StageStyle.UNDECORATED Drag the Window to have see this):

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

}



回答3:


You can just do that :

   primaryStage.setMaximized(true);



回答4:


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



回答5:


1-using JavaFX layouts (BorderPane,GridPane.... etc...)

2-add some layout constrains

you can use scene builder to generate your FXML file and resize your screen to see what's happening see this tutorial for layout

that's the way that you're telling the program to draw your components based on constrains so, in that way it will be responsive design it will follow your constrain and automatically adjust the components.



来源:https://stackoverflow.com/questions/40320199/how-to-automatically-resize-windows-in-javafx-for-different-resolutions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!