How to call setUndecorated() after a frame is made visible?

前端 未结 7 1831
醉梦人生
醉梦人生 2020-11-30 10:52

In my Swing application, I want the ability to switch between decorated and undecorated without recreating the entire frame. However, the API doesn\'t let me call setU

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 11:45

    You can't. That's been my experience when I tried to achieve the same.

    However if you have your entire UI in one panel which is in your frame, you can create a new frame and add that panel to the frame. Not so much work.

    Something like this:

    // to start with
    JPanel myUI = createUIPanel();
    JFrame frame = new JFrame();
    frame.add(myUI);
    
    // .. and later ...
    
    JFrame newFrame = new JFrame();
    newFrame.setUndecorated();
    newFrame.add(myUI);
    

    In Swing a panel (and indeed any instance of a component) can only be in one frame at a time, so when you add it to a new frame, it immediately ceases to be in the old frame.

提交回复
热议问题