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

发布于 2024-08-01 21:38:40 字数 320 浏览 5 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦中楼上月下 2024-08-08 21:38:40

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

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

我一向站在原地 2024-08-08 21:38:40

将其留在这里以帮助其他可能正在搜索此内容的人:

private int screenIndexOfMouse() {
    GraphicsDevice myScreen = MouseInfo.getPointerInfo().getDevice();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] allScreens = env.getScreenDevices();
    int myScreenIndex = -1;
    for (int i = 0; i < allScreens.length; i++) {
        if (allScreens[i].equals(myScreen))
        {
            myScreenIndex = i;
            break;
        }
    }
    
    return myScreenIndex;
}

Leaving this here to help anyone else who may be searching for this:

private int screenIndexOfMouse() {
    GraphicsDevice myScreen = MouseInfo.getPointerInfo().getDevice();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] allScreens = env.getScreenDevices();
    int myScreenIndex = -1;
    for (int i = 0; i < allScreens.length; i++) {
        if (allScreens[i].equals(myScreen))
        {
            myScreenIndex = i;
            break;
        }
    }
    
    return myScreenIndex;
}
很糊涂小朋友 2024-08-08 21:38:40

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

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

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

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

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.

陪你到最终 2024-08-08 21:38:40

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();
}

Rich's answer helped me find a whole solution:

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();
}
﹂绝世的画 2024-08-08 21:38:40

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

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

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
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文