如何将图片添加到这些JFrame?

时间:2016-05-30 19:31:54

标签: java image swing

我想将图片添加到此代码创建的每个窗口中。

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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