设置JFrame而不与任务栏重叠

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

我尝试了以下解决方案。

  • 调用setExtendedState(MAXIMIZED_BOTH)。

    • 优点 :这可以正常工作,即窗口动态调整,除了它有以下问题。
    • 问题最初窗口占据全屏虽然帧动态调整,但它与任务栏重叠。
  • 尝试下面的解决方案,如JFrame.setExtendedState(MAXIMIZED_BOTH)与未修饰的框架一起工作?

    GraphicsConfiguration config = aFrame.getGraphicsConfiguration(); Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice()); aFrame.setBounds(0, 0, usableBounds.width, usableBounds.height); 
    • 优点 :我没有重叠,窗口看起来很好。
    • 问题 :当任务栏位置/大小发生变化时,窗口不会动态调整自身。

任何帮助将不胜感激。

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

能够使用以下代码解决上述问题,

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

因此,通过getUsableBounds我可以获得离开任务栏的界限。 因此我使用setExtendedState(MAXIMIZED_BOTH) ,当我重新调整/重新定位任务栏时,窗口会自动更新。 🙂

 final Point x = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); 

有一个单独的线程来检查任务栏是否被更改。 如果是这样更新大小

 new Thread(new Runnable() { @Override public void run() { if (x.equals(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint())) { Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); setSize(r.getSize()); } try { Thread.sleep(5000); } catch (InterruptedException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); } } }).start();