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

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

最佳答案

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

关于java - 如何为这些JFrame添加图片?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37532562/

10-10 01:06