本文介绍了如何知道多屏幕环境中的JFrame是否在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的应用程序用于多屏幕环境。应用程序将其位置关闭并从最后一个位置开始。 通过调用 frame.getLocation()获得位置 这个如果该框架位于主屏幕上或位于主屏幕的右侧,则会给出正值。位于主屏幕左侧屏幕上的框架会为X获取负值。 当屏幕配置更改时(例如多个用户共享一个Citrix帐户并具有不同的屏幕分辨率),问题就会出现。 现在我的问题是要确定屏幕上是否存在存储的位置。根据其他一些帖子,我应该使用 GraphicsEnvironment 来获得可用屏幕的大小,但我无法获得不同屏幕的位置。 示例: getLocation()给出点(-250,10) GraphicsEnvironment 给出 Device1-Width:1920 Device2-Width:1280 现在,根据屏幕的顺序(次要显示器放置在主要显示器的左侧或右侧),框架可能是可见的或不可见。 您能告诉我如何解决这个问题吗? 非常感谢 $ b $但是,如果你想知道的是,如果框架可以在屏幕上看到,你可以计算出虚拟的框架, 公共类ScreenCheck { 公共职位c void main(String [] args){ JFrame frame = new JFrame(); frame.setBounds(-200,-200,200,200); Rectangle virtualBounds = getVirtualBounds(); System.out.println(virtualBounds.contains(frame.getBounds())); $ b public static Rectangle getVirtualBounds(){ Rectangle bounds = new Rectangle(0,0,0,0); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice lstGDs [] = ge.getScreenDevices(); (GraphicsDevice gd:lstGDs){ bounds.add(gd.getDefaultConfiguration()。getBounds()); } 返回界限; $ b $现在,它使用 同样,你可以使用每一个 GraphicsDevice ,但是你可以使用它的位置。 code>分别检查每一个... My application is used in multi-screen environments. The application stores it's location on close and starts on the last position.I obtain the position by calling frame.getLocation()This gives me a positive value if the frame is on the main screen or it is on the right of the main screen. Frames located on a screen left to the main screen get negative values for X.The problem comes up, when the screen configuration changes (for example multiple users share one Citrix-Account and have different screen resolutions).My problem now is to determine if the stored position is visible on the screen. According to some other posts, I should use GraphicsEnvironment to get the size of the available screens, but I can't get the position of the different screens.Example: getLocation() gives Point(-250,10)GraphicsEnvironment givesDevice1-Width: 1920Device2-Width: 1280Now, depending on the ordering of the screens (wether the secondary monitor is placed on the left or on the right of the primary one), the frame might be visible, or not.Can you tell me how to fix this problem?Thanks a lot 解决方案 This is a little redumentry, but, if all you want to know is if the frame would be visible on the screen, you could calculate the "virtual" bounds of desktop and test to see if the frame is contained within in.public class ScreenCheck { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(-200, -200, 200, 200); Rectangle virtualBounds = getVirtualBounds(); System.out.println(virtualBounds.contains(frame.getBounds())); } public static Rectangle getVirtualBounds() { Rectangle bounds = new Rectangle(0, 0, 0, 0); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice lstGDs[] = ge.getScreenDevices(); for (GraphicsDevice gd : lstGDs) { bounds.add(gd.getDefaultConfiguration().getBounds()); } return bounds; }}Now, this uses the Rectangle of the frame, but you could use it's location instead.Equally, you could use each GraphicsDevice individually and check each one in turn... 这篇关于如何知道多屏幕环境中的JFrame是否在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 20:51