2

I am making a small app, but i want to set an image as background at the whole window. I tried to make this like below but nothing happen. The image is in the folder where the class is so as a path I put only the name...Can you help me please? what can I do?

Container c = getContentPane();
  setContentPane(c);
   setContentPane(new JLabel(new ImageIcon("Chrysanthemum.jpg")));
user3233650
  • 419
  • 1
  • 4
  • 9

2 Answers2

0

One possibility is to add a BorderLayout to the JFrame, which should fill the JFrame with the JLabel, then set the background, adding the JLabel to the frame and then add components to it, like this:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Foo extends JFrame {

    public Foo() {
        setLayout(new BorderLayout());


JLabel background = new JLabel(new ImageIcon("Untitled.png"));
    add(background);
    background.setLayout(new FlowLayout());
    background.add(new JButton("foo"));
    setSize(500, 500);
    setVisible(true);
}

public static void main(String[] args) {
    Foo foo = new Foo();
}
}

The above works for me, with the JButton at the top center of the 500 by 500 JFrame with the specified background.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • it does not function...i dont know why – user3233650 Jan 25 '14 at 22:05
  • where should i have the image? I have it in the same folder with class...and I specify only the name...but it doesnt work – user3233650 Jan 25 '14 at 22:19
  • @user3233650 The approach is (mostly) correct, the possible reason it doesn't work is that the location of the image isn't where you think it is... – MadProgrammer Jan 25 '14 at 22:19
  • 1
    @user3233650 ImageIcon(String) expects to find the image relative to the context of the execution of the program. Try using new ImageIcon(getClass().getResource("imagename")) instead, for example – MadProgrammer Jan 25 '14 at 22:21
  • The default layout for `JFrame` is already `BorderLayout`. See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jan 25 '14 at 23:45
0

What I would do is create a JPanel with a background image, and add it to the JFrame. I already have a BackgroundPanel class right in one of my projects, and this is the setup I have for it.

public class MyFrame extends JFrame {

    private BackgroundPanel bgPanel;

    public MyFrame() {
        bgPanel = new BackgroundPanel("Chrysanthemum.jpg");

        setTitle("MyFrame");
        setResizable(false);
        setContentPane(bgPanel);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

// -- BackgroundPanel class

public class BackgroundPanel extends JPanel {
    private static final long serialVersionUID = 1L;

    private Image bg;

    public BackgroundPanel(String path) {
        this(Images.load(path).getImage());
    }

    public BackgroundPanel(Image img) {
        this.bg = img;
        setPreferredSize(new Dimension(bg.getWidth(null), bg.getHeight(null)));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bg != null) g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
    }
}
CoderMusgrove
  • 604
  • 8
  • 18
  • Unless you want to the background to be resizable, this is waste of effort. Also, the scaling image in this way doesn't take into consideration the ratio of the original image and doesn't generally produce a nice result - IMHO – MadProgrammer Jan 25 '14 at 22:18