My JFrame always becomes a few pixels too big.

后端 未结 1 1458
名媛妹妹
名媛妹妹 2020-12-11 23:45

I am working on this game in Java, and I just rewrote all my window related code from being based on java.awt to javax.swing. Soon after, I realized that things were a bit m

相关标签:
1条回答
  • 2020-12-12 00:28

    This is an issue related to using setResizable(false); AFTER you've tried setting the size of the window.

    The problem is that (on Windows at least), the size of the frames border is different between resizable and non-resizable windows.

    Call setResizable before calling pack

    If you want a better (and generally more reliable) way to center your window, consider using

    setLocationRelativeTo(null);
    

    instead of

    setLocation(s.width/2-getWidth()/2, s.height/2-getHeight()/2);
    

    Toolkit.getScreenSize doesn't take into consideration the space taken up by the task bar...

    0 讨论(0)
提交回复
热议问题