设置不与任务栏重叠的 JFrame

2022-01-24 00:00:00 java swing jframe

我需要一个未装饰的 JFrame(setUndecorated(true)),它需要全屏显示,不与任务栏重叠.

I need to have a undecorated JFrame(setUndecorated(true)) which need to be shown fullscreen, without overlapping with the taskbar.

我已经尝试了以下解决方案.

I have tried the below solutions.

  • 调用 setExtendedState(MAXIMIZED_BOTH).

  • Calling setExtendedState(MAXIMIZED_BOTH).

  • 优势:这可以按预期正常工作,即窗口正在动态调整,但存在以下问题.
  • 问题最初窗口占据全屏虽然框架会自行动态调整,但它会与任务栏重叠.
  • Advantage: This works fine as expected, i.e., the window is getting adjusted itself dynamically, except it has the below issues.
  • Issues Initially the window occupies the fullscreen Though the frame get adjusted itself dynamically, it overlaps with the taskbar.

尝试了 Does JFrame 中所述的以下解决方案.setExtendedState(MAXIMIZED_BOTH) 是否适用于未装饰的框架?

 GraphicsConfiguration config = aFrame.getGraphicsConfiguration();
 Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
 aFrame.setBounds(0, 0, usableBounds.width, usableBounds.height);

  • 优势:我没有重叠,窗口看起来很好.
  • 问题:更改任务栏位置/大小时,窗口不会动态调整自身.
    • Advantage: I am not getting overlaps and window looks fine.
    • Issue: Window is not adjusting itself dynamically when the taskbar position/size is changed.
    • 任何帮助将不胜感激.

      我想到了一个设计.但不确定其可行性.我可以使用 setBounds().但是,当调整或重新定位任务栏时,我需要通知我的框架.有什么办法吗?

      I thought of a design. But not sure about its feasibility. I can use the setBounds(). But then I need my frame to be notified when the task bar is adjusted or repositioned. Is there a way?

      推荐答案

      能够用下面的代码解决上述问题,

      Able to able to fix the above issue with the below code,

      Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
      setMaximizedBounds(usableBounds);
      setExtendedState(MAXIMIZED_BOTH);
      

      所以通过 getUsableBounds 我可以得到离开任务栏的边界.因此我使用 setExtendedState(MAXIMIZED_BOTH) 当我重新调整任务栏的大小/重新定位时,窗口会自动更新.:-)

      So by getUsableBounds I am able to get the bounds leaving the taskbar. And hence I am using setExtendedState(MAXIMIZED_BOTH) the window is getting updated automatically when I re-size/re-position the taskbar. :-)

相关文章