Question

I have a decorated JFrame. I've added a background image to the frame and added a JButton. When I add a button the background image is not visible. What should I do? Here is my code snippet.

public Demo3()

{
    setTitle("STARTUP");
    setSize(800,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setUndecorated(true);
    setLocationRelativeTo(null);

    JPanel jp=new JPanel();
    JButton jb=new JButton();
    JLabel jl=new JLabel(new ImageIcon("c://image.jpg"));
    jp.setLayout(null);
    jp.add(jl);
    jp.add(jb);

    add(jp);
    jb.setBounds(400,250,50,50);
    setVisible(true);
}
Was it helpful?

Solution 2

you need to set the layout of JLabel null.Then only setBounds method will work.

lj,setLaout(null);

jb.setBounds(400,250,50,50);

OTHER TIPS

Don't use a null layout.

See Background Panel for a couple of options. Depending on your exact requirement you can:

  1. use the JLabel as the background and then set the layout manager of the label
  2. do custom painting on a panel and then add components to the panel.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top