JAVA-图片打乱

package com.itheima;

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

public class shuzu09 {
    public static void main(String[] args) {
        //创建窗体对象
        JFrame jf=new JFrame();
        jf.setTitle("图片展示");
        jf.setSize(380, 400);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);


        int[][] arr ={
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}};
        Random r =new Random();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                int x = r.nextInt(arr.length);
                int y = r.nextInt(arr[x].length);
                int temp = arr[i][j];
                arr[i][j]=arr[x][y];
                arr[x][y]=temp;

            }
            
        }
        

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                JLabel jLabel =new JLabel(new ImageIcon("images\\"+arr[i][j]+".png"));
                jLabel.setBounds(j*90,i*90,90,90);
                jf.add(jLabel);
            }
        }
        jf.setVisible(true);
    }
}

posted @ 2022-10-29 15:15  NiceTwocu  阅读(25)  评论(0编辑  收藏  举报