【问题标题】:Java - How to avoid covering the taskbar with undecorated or custom LookAndFeel Window?Java - 如何避免使用未装饰或自定义 LookAndFeel 窗口覆盖任务栏?
【发布时间】:2012-06-26 16:27:03
【问题描述】:

是的,有些问题已经接近了:)

Java 中有一个错误(自 2011 年以来一直存在并被报告,似乎也没有努力修复它 - 应该在 VM 的本机端处理)

也就是说,当您最大化“未装饰”窗口或以 PLAF 外观绘制的窗口时,它将覆盖 Windows 任务栏。很好 - 当你想要它时是可取的,但是当你想要任务栏最大化时,窗口会覆盖它。设置“始终在顶部”属性没有任何区别。

是的,可以调整窗口大小,但必须知道任务栏在哪里,或者屏幕大小减去任务栏 - 知道怎么做吗?

如果这样做的话,你需要知道你在没有任务栏的屏幕上最大化。如果在多显示器虚拟桌面上...

任何想法:)

【问题讨论】:

  • bug???,您能否将BugParades链接添加到您的问题中

标签: java swing look-and-feel


【解决方案1】:

是的,可以调整窗口大小,但必须知道任务栏在哪里, 或者屏幕大小减去任务栏 - 知道该怎么做吗?

是的:

1.查找您所在的图形设备(假设 p 是您要查找的屏幕的Point):

GraphicsConfiguration graphicsConfiguration = null;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
    if (gd.getDefaultConfiguration().getBounds().contains(p)) {
        graphicsConfiguration = gd.getDefaultConfiguration();
        break;
    }
}

2.查看屏幕边界(注意某些边界位置在多个屏幕中为负值 - 例如,如果您有一个位于主屏幕左侧的辅助屏幕)、屏幕尺寸和“Insets " 的屏幕,通常是任务栏和/或其他图形工件:

Rectangle screenBounds = graphicsConfiguration.getBounds();
Dimension screenSize = screenBounds.getSize();
Insets screenInsets = Toolkit.getDefaultToolkit()
     .getScreenInsets(graphicsConfiguration);

【讨论】:

  • 谢谢 - 我必须检查屏幕的排列以及“任务栏窗口”的分配位置。看看我是否或如何找出所有这些将会很有趣。有些系统有虚拟桌面,显示跨越任务栏,有些没有,等等。
  • 对于我来说,我已经使用了GraphicsConfiguration,但我不知道screenInsets。考虑到第 2 步的逻辑效果很好。我什至在左侧有我的任务栏。
  • @Guillaume Polet,如何访问工具栏本身来设置其 Icon_Image?
  • @TiMr 你可以使用java.awt.Window.setIconImages。它适用于 Windows 和 Linux。不确定 MacOS
  • @Guillaume Polet 谢谢你的回复,我想要的是任务栏而不是工具栏,抱歉,有什么办法可以访问它。
【解决方案2】:

谢谢

这是上面的代码,在系统最大化窗口后立即调用。它检查任务栏并相应地调整窗口大小。

请注意,就 Java 而言,setBounds 将“取消最大化”窗口,因此“getExtendedState()”将返回未最大化,我需要维护自己的标志。我还必须缓存最后一个预先最大化的窗口大小,以便我知道以后将窗口恢复到哪里 - 太乱了,但它可以工作。

Rectangle bounds;
Rectangle fbounds = frame.getBounds();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

// as system maximized this at this point we test the center of the window
// as it should be on the proper screen.
Point p = new Point(fbounds.x + (fbounds.width/2),fbounds.y + (fbounds.height/2));
GraphicsConfiguration graphicsConfiguration = null;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
{
    if (gd.getDefaultConfiguration().getBounds().contains(p)) {
        graphicsConfiguration = gd.getDefaultConfiguration();
        break;
    }
}
if(graphicsConfiguration != null)
{
    bounds = graphicsConfiguration.getBounds();
    Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration);

    bounds.x += screenInsets.left;
    bounds.y += screenInsets.top;
    bounds.height -= screenInsets.bottom;
    bounds.width -= screenInsets.right;
} else {
    bounds = env.getMaximumWindowBounds();
}
if(fbounds.equals(bounds)) {
    bounds.height -= 1;
}
frame.setBounds(bounds);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多