Ste*_*eod 10 java swing multiple-monitors mouseevent
我在组件上有一个Java MouseListener来检测鼠标按下.如何判断鼠标按哪个显示器?
@Override
public void mousePressed(MouseEvent e) {
// I want to make something happen on the monitor the user clicked in
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的效果是:当用户在我的应用程序中按下鼠标按钮时,弹出窗口会显示一些信息,直到鼠标被释放.我想确保此窗口位于用户单击的位置,但我需要调整当前屏幕上的窗口位置,以便整个窗口可见.
Ric*_*ler 13
您可以从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
...
Run Code Online (Sandbox Code Playgroud)