RAJIL KV
RAJIL KV

Reputation: 399

Positioning JButton is not working

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);
}

Upvotes: 3

Views: 86

Answers (2)

user2700349
user2700349

Reputation:

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

lj,setLaout(null);

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

Upvotes: 0

camickr
camickr

Reputation: 324128

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.

Upvotes: 2

Related Questions