Java, 获取当前屏幕的窗口边界

huangapple go评论102阅读模式
英文:

Java, get Window Bounds for current screen

问题

我正在使用两台分辨率不同的显示器以下代码返回了包含我的组件的显示器

    DisplayMode dm = invoker.getGraphicsConfiguration().getDevice().getDisplayMode();

以下代码返回了屏幕的窗口边界其中考虑了任务栏

    Rectangle windowBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

问题是第二行代码始终返回主屏幕的边界但我希望在我的组件位于第二个屏幕时能获取到第二屏幕的边界有没有办法将这两行代码合并以获取当前屏幕的窗口边界

dm 具有 getHeight() 和 getWidth() 方法用于当前屏幕但它们没有考虑到 Windows 任务栏
英文:

I'm using two monitors with different resolutions. This returns the display where my component is located:

DisplayMode dm = invoker.getGraphicsConfiguration().getDevice().getDisplayMode();

And this returns window bounds of the screen, with taskbar accounted for:

Rectangle windowBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

The problem is that the second line of code always returns primary screen bounds but I want to get secondary screen bounds when my component is located on the second screen. Is there a way to combine these two lines to get current screen window bounds?

dm has getHeight() and getWidth() for current screen but they do not account for windows taskbar.

答案1

得分: 1

根据我所了解,您想要获取当前设备的屏幕尺寸,以便个性化您的应用程序尺寸。您可以使用Java Toolkit类来实现这一点。

首先获取并存储屏幕尺寸:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

然后将屏幕的宽度和高度存储为整数:

// 屏幕宽度
int screenWidth = (int) screenSize.getWidth();
// 屏幕高度
int screenHeight = (int) screenSize.getHeight();

在将宽度和高度存储为整数之后,您可以像对待其他原始变量一样使用和比较它们。

英文:

From what I can tell, you want to get the screen size of the current device in order to personalize your applications dimensions.
You can do this with Java Toolkit class.

First get and store the screen size:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Then store the width and height of the screen in integers:

// screen size width
int screenWidth = screenSize.getWidth;
// screen size height
int screenHeight = screenSize.getHeight;

After you have stored the width and height into integers, you can then use and compare them like one would with other primitive variables.

huangapple
  • 本文由 发表于 2020年9月16日 17:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63917494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定