Background image in a JFrame

后端 未结 3 1470
一个人的身影
一个人的身影 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 00:45

    Look in the documentation on the Root Pane for all the information you need. Note the availability of the layered pane and the glass pane as well as the content pane.

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