如何确定Swing鼠标事件发生在哪个监视器?

编程入门 行业动态 更新时间:2024-10-21 17:42:07
本文介绍了如何确定Swing鼠标事件发生在哪个监视器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在组件上有一个Java MouseListener来检测鼠标按下。如何判断鼠标按哪个显示器?

I have a Java MouseListener on a component to detect mouse presses. How can I tell which monitor the mouse press occurred in?

@Override public void mousePressed(MouseEvent e) { // I want to make something happen on the monitor the user clicked in }

我想要实现的效果是:当用户在我的应用程序中按下鼠标按钮时,弹出窗口会显示一些信息,直到鼠标被释放。我想确保此窗口位于用户点击的位置,但我需要调整当前屏幕上的窗口位置,以便整个窗口可见。

The effect I'm trying to achieve is: when the user presses the mouse button in my app, a popup window shows some info, until the mouse is released. I want to ensure this window is positioned where the user clicks, but I need to adjust the window position on the current screen so that the entire window is visible.

推荐答案

您可以从 java.awt.GraphicsEnvironment 。您可以使用它来获取有关本地系统的信息。包括每个监视器的边界。

You can get display information from java.awt.GraphicsEnvironment. You can use this to get a information about your local system. Including the bounds of each monitor.

Point point = event.getPoint(); GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] devices = e.getScreenDevices(); Rectangle displayBounds = null; //now get the configurations for each device for (GraphicsDevice device: devices) { GraphicsConfiguration[] configurations = device.getConfigurations(); for (GraphicsConfiguration config: configurations) { Rectangle gcBounds = config.getBounds(); if(gcBounds.contains(point)) { displayBounds = gcBounds; } } } if(displayBounds == null) { //not found, get the bounds for the default display GraphicsDevice device = e.getDefaultScreenDevice(); displayBounds =device.getDefaultConfiguration().getBounds(); } //do something with the bounds ...

更多推荐

如何确定Swing鼠标事件发生在哪个监视器?

本文发布于:2023-11-11 06:11:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   监视器   事件   发生在   Swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!