Java: how to register a listener that listen to a JFrame movement

后端 未结 3 1994
死守一世寂寞
死守一世寂寞 2021-01-20 02:55

How can you track the movement of a JFrame itself? I\'d like to register a listener that would be called back every single time JFrame.getLocation() is going to

相关标签:
3条回答
  • 2021-01-20 03:12

    You can register a HierarchyBoundsListener on your JFrame, or use a ComponentListener as suggested by others.

    jf.getContentPane().addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
    
        @Override
        public void ancestorMoved(HierarchyEvent e) {
            updateText(jf, jl);
        }
    });
    
    0 讨论(0)
  • 2021-01-20 03:14
    JFrame jf = new JFrame();
    jf.addComponentListener(new ComponentListener() {...});
    

    is what you are looking for, I think.

    0 讨论(0)
  • 2021-01-20 03:21

    Using addComponentListener() with a ComponentAdapter:

    jf.addComponentListener(new ComponentAdapter() {
        public void componentMoved(ComponentEvent e) {
            updateText(jf, jl);
        }
    });
    
    0 讨论(0)
提交回复
热议问题