简体   繁体   中英

How to make a JFrame not movable?

The problem

I would like to know whether it is possible to make a JFrame not movable/draggable - just out of curiosity and not for a real use case. The problem is that the window manager of the operating system is responsible for the appearance of the windows, but the program itself has only a few possible settings. This is why I am looking for a hack.

What I have tried

  • removed the decoration of the JFrame as stated in this answer (however, it is still possible to move the window, eg by holding the ALT key on GNU/Linux)
  • set a ComponentListener which resets the position if the JFrame is moved (however, this looks very glitchy since the listener only reacts after the dragging, but the JFrame should always stay at the same position)
  • created a full screen JFrame , made it transparent and added a JPanel to simulate a window (however, one can still drag the window, even if it is maximized)
  • used a JWindow and a JDialog instead (also does not work)

Is there any way to achieve the desired behavior by using a different approach or improving one of my approaches?

private void formComponentMoved(java.awt.event.ComponentEvent evt) {                                    
       this.setLocationRelativeTo(null);
    }     

addComponentListener(new java.awt.event.ComponentAdapter() {
                public void componentMoved(java.awt.event.ComponentEvent evt) {
                    formComponentMoved(evt);
                }
            });

Works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM