设置JFrame窗口不与任务栏重叠

4

我需要一个没有装饰的JFrame(setUndecorated(true)),它需要全屏显示,而且不能与任务栏重叠。

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

  • 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.
  • Tried the below solution as stated in Does JFrame.setExtendedState(MAXIMIZED_BOTH) work with undecorated frames?

     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()方法,但是我需要在任务栏被调整或重新定位时通知我的框架。有什么方法吗?


我不确定有没有办法实现这个,至少不去到本地水平。 - MadProgrammer
@MadProgrammer 不行,你可以使用GraphicsEnvironment类来实现。请查看我的解决方案。 - Thusitha Thilina Dayaratne
@Thuiya 不,当任务栏被更改时它不会自动更新。 - MadProgrammer
@MadProgrammer 我们可以创建一个线程来检查屏幕尺寸是否发生变化。 - Thusitha Thilina Dayaratne
请注意,尽管在Windows操作系统中未装饰的窗口最大化时会覆盖任务栏,但我认为其他操作系统不会如此。 - Boann
显示剩余4条评论
2个回答

6
能够使用以下代码修复上述问题:
Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
setMaximizedBounds(usableBounds);
setExtendedState(MAXIMIZED_BOTH);

因此,通过使用 getUsableBounds 方法,我可以获取除任务栏之外的边界。因此,当我重新调整/重新定位任务栏时,我使用 setExtendedState(MAXIMIZED_BOTH) 方法自动更新窗口。:-)


在一些系统上它对我不起作用,问题出在Windows 10上,显示设置的缩放和布局设置为125%,将其更改为100%后它开始工作。在JDK 11上进行了测试。 - Aqeel Haider

0
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();

setSize() 没有起作用。与 setBounds() 相同的行为。当我改变任务栏的大小/位置时,窗口大小不会动态更新。 - Kannan Ramamoorthy
你是在建议创建一个无限运行的线程,并根据任务栏更新框架大小吗? - Kannan Ramamoorthy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接