Setting a JFrame without overlapping with taskbar

我的未来我决定 提交于 2019-12-02 00:10:27

问题


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.

  • 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.

Any help would be greatly appreciated.

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?


回答1:


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

Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
setMaximizedBounds(usableBounds);
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. :-)




回答2:


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

Have a separate thread to check whether taskbar get changed. If so update size

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


来源:https://stackoverflow.com/questions/19632093/setting-a-jframe-without-overlapping-with-taskbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!