我想要一个Swing应用程序,它将从文件夹中随机选取一些图像并显示它们。如何在Swing中显示随机图像
我试过这样的东西,但图像不呈现。
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class RandomCards extends JFrame
{
RandomCards()
{
setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));
Map<Integer, String> hm = new HashMap<Integer, String>();
int noOfImage=3;
for(int i=0; i < noOfImage; i++)
{
hm.put(i, "resources/" + i + ".png");
}
double cardNumber = Math.floor(Math.random()*3) + 1;
add(new JLabel(hm.get(cardNumber)));
}
public static void main (String [] args)
{
RandomCards frame = new RandomCards();
frame.setSize(330, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
另请参见['Collections.shuffle()'](http://download.oracle.com/javase/6/docs/api /java/util/Collections.html)。 – trashgod 2011-05-31 16:45:43