0

I want to add picture to every window that this code creates.

import java.util.Random;
import javax.swing.JFrame;

public class what {
    public static void main(String[] args) {
        Random ran = new Random();
        while(true) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setName("" +ran.nextLong());
            frame.setBounds(ran.nextInt(700), ran.nextInt(890) + 10, ran.nextInt(700), ran.nextInt(700));
            JFrame frame1 = new JFrame();
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setVisible(true);
            frame1.setName("" + ran.nextLong());
            frame1.setBounds(ran.nextInt(700), ran.nextInt(890) + 10, ran.nextInt(700), ran.nextInt(700));
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Puciek
  • 1
  • Don't call `setBounds()` on frames (or any other JComponent). Let the window work out its own size, or call `setSize()`, not `setBounds()`. – markspace May 30 '16 at 19:53
  • 1
    [How to use labels](https://docs.oracle.com/javase/tutorial/uiswing/components/label.html) – MadProgrammer May 30 '16 at 20:51
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Do you intend to use the images as a background for other components added on top? – Andrew Thompson May 30 '16 at 23:51

2 Answers2

1
JLabel imgLabel = new JLabel(new ImageIcon("path_to_image.png"));
add(imgLabel)
1

frame.add(new JLabel(new ImageIcon("Image Path/Image.png")));

VIP
  • 35
  • 6