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

时间:2021-10-27 11:31:24

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

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

@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.

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

4 个解决方案

#1


13  

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.

您可以从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
...

#2


2  

Rich's answer helped me find a whole solution:

Rich的回答帮助我找到了一个完整的解决方案:

public void mousePressed(MouseEvent e) {
    final Point p = e.getPoint();
    SwingUtilities.convertPointToScreen(p, e.getComponent());
    Rectangle bounds = getBoundsForPoint(p);
    // now bounds contains the bounds for the monitor in which mouse pressed occurred
    // ... do more stuff here
}


private static Rectangle getBoundsForPoint(Point point) {
    for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
        for (GraphicsConfiguration config : device.getConfigurations()) {
            final Rectangle gcBounds = config.getBounds();
            if (gcBounds.contains(point)) {
                return gcBounds;
            }
        }
    }
    // if point is outside all monitors, default to default monitor
    return GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
}

#3


1  

Since Java 1.6 you can use getLocationOnScreen, in previous versions you must get the location of the component that generated the event:

从Java 1.6开始,您可以使用getLocationOnScreen,在以前的版本中,您必须获取生成事件的组件的位置:

Point loc;
// in Java 1.6
loc = e.getLocationOnScreen();
// in Java 1.5 or previous
loc = e.getComponent().getLocationOnScreen();

You will have to use the GraphicsEnvironment class to get the bound of the screen.

您必须使用GraphicsEnvironment类来获取屏幕的边界。

#4


0  

Maybe e.getLocationOnScreen(); will work? It's only for java 1.6.

也许e.getLocationOnScreen();将工作?它仅适用于java 1.6。

#1


13  

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.

您可以从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
...

#2


2  

Rich's answer helped me find a whole solution:

Rich的回答帮助我找到了一个完整的解决方案:

public void mousePressed(MouseEvent e) {
    final Point p = e.getPoint();
    SwingUtilities.convertPointToScreen(p, e.getComponent());
    Rectangle bounds = getBoundsForPoint(p);
    // now bounds contains the bounds for the monitor in which mouse pressed occurred
    // ... do more stuff here
}


private static Rectangle getBoundsForPoint(Point point) {
    for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
        for (GraphicsConfiguration config : device.getConfigurations()) {
            final Rectangle gcBounds = config.getBounds();
            if (gcBounds.contains(point)) {
                return gcBounds;
            }
        }
    }
    // if point is outside all monitors, default to default monitor
    return GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
}

#3


1  

Since Java 1.6 you can use getLocationOnScreen, in previous versions you must get the location of the component that generated the event:

从Java 1.6开始,您可以使用getLocationOnScreen,在以前的版本中,您必须获取生成事件的组件的位置:

Point loc;
// in Java 1.6
loc = e.getLocationOnScreen();
// in Java 1.5 or previous
loc = e.getComponent().getLocationOnScreen();

You will have to use the GraphicsEnvironment class to get the bound of the screen.

您必须使用GraphicsEnvironment类来获取屏幕的边界。

#4


0  

Maybe e.getLocationOnScreen(); will work? It's only for java 1.6.

也许e.getLocationOnScreen();将工作?它仅适用于java 1.6。