JComponent size issue

醉酒当歌 提交于 2019-12-02 02:05:46

Override getPreferredSize() in your JComponent to return your preferred size, and start with half the width and height of that Dimension. To the same end, this KineticModel invokes setPreferredSize() in DisplayPanel.

Addendum: By way of explanation, your current approach fails because the results from getWidth() and getHeight() are invalid until validate() has been called on the enclosing container, typically as the result of pack().

I agree with trashgod's answer. (+1)

Move the ballX and ballY in the paintComponent(g) like this

if (ballX==0 && ballY==0) {
    ballX = getWidth()/2;
    ballY = getHeight()/2;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!