本文介绍了如何设置的Java Swing present屏幕尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与摆动API工作第一次为我的Windows应用程序的屏幕分辨率。

I have working with swing API for the first time to get the screen resolution for my win application.

如何设置present屏幕尺寸中的Java Swing?

How to set present screensize in Java Swing?

推荐答案

这个问题要看情况。你想在全屏幕大小或可用的屏幕尺寸(减去东西,如任务栏和停靠处)?你想默认的屏幕或特定的屏幕?

The question depends. Do you want the full screen size or the usable screen size (minus things like the taskbar and the dock)? Do you want the default screen or a specific screen?

首先,你需要获取到当前的参考 GraphicsEnvironment中 ...

First you need to grab a reference to the current GraphicsEnvironment...

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

然后你可以抓住的系统...

Then you can grab a list of the GraphicsDevices available on the system...

GraphicsDevice lstGDs[] = ge.getScreenDevices();

或者,如果你只是想在当前/默认屏幕,可以使用...

Or if you just want the current/default screen, you can use...

GraphicsDevice device = ge.getDefaultScreenDevice();

总之,这无论你选择,接下来,你需要获得的GraphicsConfiguration 从设备......

GraphicsConfiguration cf = device.getDefaultConfiguration();

最后,您可以抓取屏幕边界,这包括虚拟桌面中的屏幕的位置和大小...

And finally, you can grab the screen bounds, this includes the position and size of the screen within the virtual desktop...

Rectangle bounds = cf.getBounds();

安全/可视屏幕尺寸...

这考虑到东西,如任务栏或停靠或操作系统可能有其他UI元素在屏幕上,你不应该试图和出现超/后面。不是每个人都有这些在屏幕的底部;)

Safe/viewable screen size...

This takes into consideration things like the task bar or dock or other UI elements the OS might have on the screen, which you shouldn't try and appear over/behind. Not everybody has these at the bottom of the screen ;)

通过抓住启动的GraphicsConfiguration 您选择了的GraphicsDevice ,从这个,让屏幕边界...

Start by grabbing your GraphicsConfiguration from your selected GraphicsDevice, from this, get the screen bounds...

GraphicsDevice device = ge.getDefaultScreenDevice(); // or what ever device you want...
GraphicsConfiguration cf = device.getDefaultConfiguration();
Rectangle bounds = cf.getBounds();

接下来,让屏幕插图...

Next, get the screen insets...

Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

这再presents由操作系统,以显示其额外的东西占用的面积。现在,由插图减少屏幕边界...

This represents the area taken up by the OS to display its extra stuff. Now, reduce the screen bounds by the insets...

bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);

这再presents安全/可视区域,你可以窗口没有被阻止本等元素出现在...

This represents the safe/viewable area you window can appear in without been blocked by this other elements...

或者你可以只使用,并把它传递 JFrame.MAXIMIZED_BOTH 这会照顾它一切为你..

Or you could just use JFrame#setExtendState and pass it JFrame.MAXIMIZED_BOTH which will take care of it all for you...

这篇关于如何设置的Java Swing present屏幕尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:35