我注意到如果屏幕模式在Java之外被更改,java.awt.GraphicsEnvironment.getMaximumWindowBounds()
仍会在屏幕尺寸更改之前返回尺寸。
但是java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
可以正常工作并显示新的解决方案。
我跟踪了getMaximumBounds()
对GraphicsDevice.getDefaultConfiguration()
的调用,我怀疑他正在使用过时的配置。
我的问题是:有没有办法告诉Java屏幕配置是否已更改,或者这只是一个错误?
感谢。
这是一个显示错误的简单应用程序:
import java.awt.*;
public class test {
public static void print() {
try {
Rectangle r = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
System.out.println("max window bounds=" + r);
DisplayMode mode = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
System.out.println("mode=" + mode.getWidth() + "x" + mode.getHeight());
GraphicsConfiguration gc = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
System.out.println("insets=" + insets);
} catch (Exception e) {}
}
public static void main(String args[]) {
try {
print();
System.out.println("Change video mode and press Enter to continue...");
System.in.read();
print();
} catch (Exception e) {}
}
}
答案 0 :(得分:1)
这是一个适用于我的解决方法:
public static Rectangle getMaximumBounds() {
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
DisplayMode mode = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
Rectangle bounds = new Rectangle();
bounds.x = insets.left;
bounds.y = insets.top;
bounds.width = mode.getWidth() - (insets.left + insets.right);
bounds.height = mode.getHeight() - (insets.top + insets.bottom);
return bounds;
}
但我注意到其他awt功能因屏幕模式改变而无法调整。 JPopupMenu.show()将菜单关闭。 Pffff ....好吧......
答案 1 :(得分:0)
根据Peter Quiring的回答,我根据组件的位置添加了对多个显示器的支持。
public static Rectangle getMaximumBounds(Component comp) {
GraphicsConfiguration gc = comp.getGraphicsConfiguration();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
DisplayMode mode = gc.getDevice().getDisplayMode();
int xPos = 0;
int yPos = 0;
for(GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
Rectangle deviceBounds = device.getDefaultConfiguration().getBounds();
if(deviceBounds.contains(gc.getBounds())) {
xPos = gc.getBounds().x;
yPos = gc.getBounds().y;
}
}
Rectangle bounds = new Rectangle();
bounds.x = insets.left + xPos;
bounds.y = insets.top + yPos;
bounds.width = mode.getWidth() - (insets.left + insets.right);
bounds.height = mode.getHeight() - (insets.top + insets.bottom);
return bounds;
}