java中屏幕宽度的代码_你如何获得在java中的屏幕宽度?

这是Lawrence Dol发表的多监视器解决scheme的改进。 正如在他的解决scheme中,这个代码占多个监视器和任务栏插页。 包含的函数是: getScreenInsets() , getScreenWorkingArea()和getScreenTotalArea() 。

Lawrence Dol版本的变化:

这避免了两次获取graphicsconfiguration。

增加了获取总屏幕区域的function。

为了清楚起见,将variables重命名。

增加了Javadocs。

码:

/** * getScreenInsets, This returns the insets of the screen, which are defined by any task bars * that have been set up by the user. This function accounts for multi-monitor setups. If a * window is supplied, then the the monitor that contains the window will be used. If a window * is not supplied, then the primary monitor will be used. */ static public Insets getScreenInsets(Window windowOrNull) { Insets insets; if (windowOrNull == null) { insets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment .getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration()); } else { insets = windowOrNull.getToolkit().getScreenInsets( windowOrNull.getGraphicsConfiguration()); } return insets; } /** * getScreenWorkingArea, This returns the working area of the screen. (The working area excludes * any task bars.) This function accounts for multi-monitor setups. If a window is supplied, * then the the monitor that contains the window will be used. If a window is not supplied, then * the primary monitor will be used. */ static public Rectangle getScreenWorkingArea(Window windowOrNull) { Insets insets; Rectangle bounds; if (windowOrNull == null) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice() .getDefaultConfiguration()); bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds(); } else { GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration(); insets = windowOrNull.getToolkit().getScreenInsets(gc); bounds = gc.getBounds(); } bounds.x += insets.left; bounds.y += insets.top; bounds.width -= (insets.left + insets.right); bounds.height -= (insets.top + insets.bottom); return bounds; } /** * getScreenTotalArea, This returns the total area of the screen. (The total area includes any * task bars.) This function accounts for multi-monitor setups. If a window is supplied, then * the the monitor that contains the window will be used. If a window is not supplied, then the * primary monitor will be used. */ static public Rectangle getScreenTotalArea(Window windowOrNull) { Rectangle bounds; if (windowOrNull == null) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds(); } else { GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration(); bounds = gc.getBounds(); } return bounds; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值