Background image in a JFrame

后端 未结 3 1479
一个人的身影
一个人的身影 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:26

    When using null layout (and you almost never should) you have to supply a bounds for every component, otherwise it defaults to (0 x,0 y,0 width,0 height) and the component won't display.

    BackgroundPanel bg = new BackgroundPanel();
    cp.add(bg);
    

    isn't supplying a bounds. You'll need to do something like:

    BackgroundPanel bg = new BackgroundPanel();
    bg.setBounds(100, 100, 100, 100);
    cp.add(bg);
    

    Which would make bg size 100 x 100 and place it at 100 x, 100 y on the frame.

提交回复
热议问题