Pergunta

Eu tenho um Java MouseListener em um componente para detectar prensas rato. Como posso saber que monitoram a imprensa rato ocorreu em?

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

O efeito que eu estou tentando alcançar é: quando o usuário pressiona o botão do rato em meu aplicativo, uma janela popup mostra algumas informações, até que o mouse é liberado. Eu quero garantir que esta janela está posicionado onde o usuário clica, mas eu preciso para ajustar a posição da janela na tela atual para que toda a janela é visível.

Foi útil?

Solução

Você pode obter informações de exibição de java .awt.GraphicsEnvironment . Você pode usar isso para obter uma informação sobre seu sistema local. Incluindo os limites de cada 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
...

Outras dicas

A resposta de rico me ajudou a encontrar uma solução inteira:

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

Desde Java 1.6 você pode usar getLocationOnScreen, em versões anteriores, você deve obter a localização do componente que gerou o evento:

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

Você terá que usar a classe GraphicsEnvironment para obter o limite da tela.

Talvez e.getLocationOnScreen (); vai funcionar? É só para Java 1.6.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top