加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:06:29 所属栏目:Java 来源:网络整理
导读:我在组件上有一个 Java MouseListener来检测鼠标按压.如何知道鼠标按下哪个显示器? @Overridepublic void mousePressed(MouseEvent e) { // I want to make something happen on the monitor the user clicked in} 我试图实现的效果是:当用户在我的应用程
我在组件上有一个 Java MouseListener来检测鼠标按压.如何知道鼠标按下哪个显示器?
@Override
public void mousePressed(MouseEvent e) {
  // I want to make something happen on the monitor the user clicked in
}

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

解决方法

您可以从 java.awt.GraphicsEnvironment获取显示信息.您可以使用它来获取有关本地系统的信息.包括每个监视器的界限.
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
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读