Infinite disappear-reappear loop in JFrame java

后端 未结 1 1013
一向
一向 2021-01-27 20:30

As a succession to this post I\'m running into an infinite loop that causes my computer to crash. Or, well, not quite: it seems to cause the Java window (JFrame) to keep getting

相关标签:
1条回答
  • 2021-01-27 21:18

    I'm not entirely sure why you are invoking both

        getInstance().setExtendedState(JFrame.ICONIFIED);
        getInstance().setExtendedState(JFrame.NORMAL);
    

    but if you remove the first, i.e. simply:

        getInstance().setExtendedState(JFrame.NORMAL);
    

    then it just opens the window once, without the infinite looping you describe.

    The reason your code causes an infinite loop is that it minimizes and then unminimizes the frame, which results in the windowActivated method being run: this invokes the refresh method, which invokes the bringToFront method, which minimizes and unminimizes the frame etc.


    To actually bring the window to the front, the following works for me, based upon this question:

    private static void bringToFront() {                                                             
        getInstance().setVisible(true);                                                              
        getInstance().setExtendedState(JFrame.NORMAL);                                               
        getInstance().toFront();                                                                     
        getInstance().repaint();                                                                     
    }
    
    0 讨论(0)
提交回复
热议问题