这个问题 https://stackoverflow.com/questions/6422931/why-jframe-hides-taskbar-when-maximized讨论 JFrames 扩展到 Windows 任务栏的一个已知错误。一个答案 https://stackoverflow.com/a/6422995/411282错误报告的链接(其中有各种重复项)并提供了解决方法。我发现这个问题也适用于 JDialogs。 JFrame 解决方法不适用。是否有类似的解决方法可以使 JDialogs 在 Windows 上正常运行?
示例代码:
import javax.swing.*;
public class Demo extends JDialog {
public Demo() {
setSize(250,12500);
setVisible(true);
}
public static void main(String[] args) {
new Demo();
}
}
Edit:
看起来这个问题不会在 JDK 中得到修复。此错误报告 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6798063最后评论道:“如果开发人员希望他们的窗口在屏幕上完全可见,他们应该考虑自己检查屏幕插图[就像下面的解决方案],并以不同的方式重新布局其组件,或者在调用 pack() 后手动重新调整窗口大小,或者使用屏幕插入感知布局管理器[与 BorderLayout 和 GridBagLayout 等更常见的布局不同]."
这将基本上确保对话框“适合”指定的屏幕,方法是将其移动到指定设备的边界内并缩小它,使其左边缘和底部边缘位于指定设备的边界内。
这会查看设备边界和插入来计算对话框可以驻留的“安全”区域。
public class TestScreenSize {
public static void main(String[] args) {
new TestScreenSize();
}
public TestScreenSize() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
Test test = new Test();
test.setLayout(new BorderLayout());
test.setVisible(true);
System.exit(0);
}
});
}
public class Test extends JDialog {
public Test() {
setModal(true);
setLocation(0, 0);
setSize(2000, 2000);
}
@Override
public void setBounds(int x, int y, int width, int height) {
Rectangle bounds = getSafeScreenBounds(new Point(x, y));
if (x < bounds.x) {
x = bounds.x;
}
if (y < bounds.y) {
y = bounds.y;
}
if (width > bounds.width) {
width = (bounds.x + bounds.width) - x;
}
if (height > bounds.height) {
height = (bounds.y + bounds.height) - y;
}
super.setBounds(x, y, width, height);
}
}
public static Rectangle getSafeScreenBounds(Point pos) {
Rectangle bounds = getScreenBoundsAt(pos);
Insets insets = getScreenInsetsAt(pos);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
return bounds;
}
public static Insets getScreenInsetsAt(Point pos) {
GraphicsDevice gd = getGraphicsDeviceAt(pos);
Insets insets = null;
if (gd != null) {
insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
}
return insets;
}
public static Rectangle getScreenBoundsAt(Point pos) {
GraphicsDevice gd = getGraphicsDeviceAt(pos);
Rectangle bounds = null;
if (gd != null) {
bounds = gd.getDefaultConfiguration().getBounds();
}
return bounds;
}
public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
GraphicsDevice device = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
for (GraphicsDevice gd : lstGDs) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screenBounds = gc.getBounds();
if (screenBounds.contains(pos)) {
lstDevices.add(gd);
}
}
if (lstDevices.size() > 0) {
device = lstDevices.get(0);
} else {
device = ge.getDefaultScreenDevice();
}
return device;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)