Background image in a JFrame

后端 未结 3 1478
一个人的身影
一个人的身影 2020-12-04 00:15

This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paint

3条回答
  •  無奈伤痛
    2020-12-04 00:43

    By default all components have a 0 size. Just because you do some painting on a component doesn't give the component a size. You are still responsible for setting the size. That is why you should always use a layout manager. It looks after all this size stuff for you so you don't have to worry.

    I don't know why newbies always think they can't use a layout manager. Yes it takes a couple of more minutes to learn, but it saves you a lot of grief in the long run.

    Background Panel shows a couple of approaches. Again they both assume you use a layout manager, so you may need to set the size manually.

提交回复
热议问题