Как я определяю, в каком мониторе событие от нажатия мыши Swing происходит?

CSS

#loading { background: url(/image/loading.gif) no-repeat center; }
JavaScript

$.listen('click', '#captcha', function() {
    $('#captcha-block').html('<div id="loading" style="width: 70px; height: 40px; display: inline-block;" />');
    $.get("/captcha/new", null, function(data) {
        $('#captcha-block').html(data);
    }); 
    return false;
});
10
задан Steve McLeod 8 August 2009 в 09:01
поделиться

4 ответа

Информацию об отображении можно получить из 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
...
13
ответ дан 3 December 2019 в 22:01
поделиться

Ответ Рича помог мне найти полное решение:

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();
}
2
ответ дан 3 December 2019 в 22:01
поделиться

Начиная с Java 1.6, вы можете использовать getLocationOnScreen, в предыдущих версиях вы должны были получить местоположение компонента, который сгенерировал событие:

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

Вам нужно будет использовать класс GraphicsEnvironment, чтобы получить границу экран.

1
ответ дан 3 December 2019 в 22:01
поделиться

Может быть, e.getLocationOnScreen (); будет работать? Только для java 1.6.

0
ответ дан 3 December 2019 в 22:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: