Why can't I access my panel's getWidth() and getHeight() functions?

后端 未结 3 1214
长情又很酷
长情又很酷 2020-12-03 23:11

I\'m writing a simple program to test out basic GUI. The program prints a letter in the middle of the screen and allows the user to move it with the arrow keys. Everything w

3条回答
  •  心在旅途
    2020-12-03 23:33

    Member variables are initialized when the object is created (after the constructors of any parent classes & before constructor body of the current class. Swing does not doing any sizing work when the object is first created. Therefore, when you call getWidth() and getHeight(), no values have been set yet.

提交回复
热议问题