即使DisplayDevice不是Java中的FullScreen,如何获得全屏显示器大小?

问题描述 投票:0回答:1

我正在寻找一种Java方法,可以全屏显示尺寸。使用时,

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();

要么

double width = GraphicsEnvironment.getLocalGraphicsEnvironment().
    getMaximumWindowBounds().getWidth();

只要屏幕改变分辨率,宽度和高度值就会改变。我不理解这种测量,因为宽度和高度值是基本类型double(这意味着它们位于堆栈中)。

经过详尽的搜索后,我不得不求助于这个论坛,因为我找不到像availSize这样的方法

java javafx javafx-8 fullscreen screen-size
1个回答
0
投票

您可以使用以下方法全屏显示所有内容,也可以调用静态方法来更改仅传递FXML名称的场景(another.fxml)。希望你是在找这个:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;

import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends Application {


    private static Stage mainStage;

    public static Stage getStage() {
        return mainStage;
    }

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

        mainStage = stage;


        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));

        Region contentRootRegion = (Region) loader.load();


        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        double origW = d.width;
        double origH = d.height;

        // Set a default "standard" or "100%" resolution
        // origW = 1360;
        // origH = 700;

        // If the Region containing the GUI does not already have a preferred width and height, set it.
        // But, if it does, we can use that setting as the "standard" resolution.
        if (contentRootRegion.getPrefWidth() == Region.USE_COMPUTED_SIZE)
            contentRootRegion.setPrefWidth(origW);
        else
            origW = contentRootRegion.getPrefWidth();

        if (contentRootRegion.getPrefHeight() == Region.USE_COMPUTED_SIZE)
            contentRootRegion.setPrefHeight(origH);
        else
            origH = contentRootRegion.getPrefHeight();

        //Wrap the resizable content in a non-resizable container (Group)
        Group group = new Group(contentRootRegion);
        //Place the Group in a StackPane, which will keep it centered
        StackPane rootPane = new StackPane();
        rootPane.getChildren().add(group);


        //Create the scene iniitally at the "100%" size
        Scene scene = new Scene(rootPane, origW, origH);
        //Bind the scene's width and height to the scaling parameters on the group
        group.scaleXProperty().bind(scene.widthProperty().divide(origW));
        group.scaleYProperty().bind(scene.heightProperty().divide(origH));
        //Set the scene to the window (stage) and show it

        stage.setTitle("My App Name");

        Screen screen = Screen.getPrimary();
        Rectangle2D bounds = screen.getVisualBounds();

        stage.setX(bounds.getMinX());
        stage.setY(bounds.getMinY());
        stage.setWidth(bounds.getWidth());
        stage.setHeight(bounds.getHeight());
        stage.setMaximized(true);

        //stage.setFullScreen(true);

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


    public static void ChangeScene(String FXML) {
        try {
            FXMLLoader loader = new FXMLLoader(Main.class.getResource(FXML));
            Region contentRootRegion = (Region) loader.load();


            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            double origW = d.width;
            double origH = d.height;

            if (contentRootRegion.getPrefWidth() == Region.USE_COMPUTED_SIZE)
                contentRootRegion.setPrefWidth(origW);
            else
                origW = contentRootRegion.getPrefWidth();

            if (contentRootRegion.getPrefHeight() == Region.USE_COMPUTED_SIZE)
                contentRootRegion.setPrefHeight(origH);
            else
                origH = contentRootRegion.getPrefHeight();

            //Wrap the resizable content in a non-resizable container (Group)
            Group group = new Group(contentRootRegion);
            //Place the Group in a StackPane, which will keep it centered
            StackPane rootPane = new StackPane();
            rootPane.getChildren().add(group);


            //Create the scene initially at the "100%" size
            Scene scene = new Scene(rootPane, origW, origH);
            //Bind the scene's width and height to the scaling parameters on the group
            group.scaleXProperty().bind(scene.widthProperty().divide(origW));
            group.scaleYProperty().bind(scene.heightProperty().divide(origH));
            //Set the scene to the window (stage) and show it

            Screen screen = Screen.getPrimary();
            Rectangle2D bounds = screen.getVisualBounds();

            Main.getStage().setX(bounds.getMinX());
            Main.getStage().setY(bounds.getMinY());
            Main.getStage().setWidth(bounds.getWidth());
            Main.getStage().setHeight(bounds.getHeight());
            //Main.getStage().setMaximized(true);

            Main.getStage().setScene(scene);
            Main.getStage().show();


        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.