简体   繁体   中英

Java Mac OS X - Programmatically compute screen height without the Dock height

I need this to make the application open in full height-width (without full screen), but the size should be exact without the dock (setting the size of a javafx.scene.Scene so big creates some problems in the title double clicking behaviour).

I tried with this code:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return new Dimension(screenSize.width, screenSize.height);

It works but it considers the Dock as part of the screen. Is there a way to programmatically compute screen height without the Dock height or at least to compute the size of the dock?


I also tested this solution but the strange double click behaviour continues to persist :

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("title");
        Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        Group root = new Group();
        Scene scene = new Scene(root, bounds.getWidth(), bounds.getHeight(), Color.WHITE);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

For AWT and Swing, you can get the appropriate size for a window by using the class GraphicsEnvironment . Although a machine may have many graphics environments, you get access to the screen by calling the static method getLocalGraphicsEnvironment .

Then you can use the getMaximumWindowBounds() . This gives you the boundaries, including top, left, bottom and right, to which you should limit your window. Note that it's not just a matter of width and height: The Dock can be located on the left or on the right. And it also takes the menu bar into account, which Toolkit.getScreenSize doesn't. So take care to also set the origin of your window properly.

java.awt.Rectangle bounds = GraphicsEnvironment
                            .getLocalGraphicsEnvironment()
                            .getMaximumWindowBounds();

For JavaFX, you can use the Screen class. In this case, you use:

javafx.geometry.Rectangle2D bounds = Screen.getPrimary().getVisualBounds();

The returned Rectangle2D contains maxX and maxY , which are the width and height of the complete screen, and width and height that are what you need - the values with the dock and menu bar eliminated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM