我可以用图像列表创建一个JFrame吗?
问题描述:
我想用Java从Web下载的图像列表创建一个JFrame。我会把它们放在下面的JFrame中,并将图像放在文本的旁边,我该怎么做?我可以用图像列表创建一个JFrame吗?
我做了什么:
Image image = null;
ArrayList<JLabel> lb = new ArrayList<JLabel>(); // list of images
JFrame frame = new JFrame();
frame.setSize(300, 300);
lb.add(...);
//...
frame.add(lb);
frame.setVisible(true);
答
您可以使用网格布局
ArrayList<JLabel> lb=new ArrayList<JLabel>(); //list of images
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(rows,columns));//In your case (lb.size,2)
frame.setSize(300, 300);
//Now You need to Iterate through the List.
for(JLabel label:lb){
frame.add(lb); //Adding each image to the Frame
frame.add(textLabel); //This is the text you want in side of image
}
frame.setVisible(true);
正如@让·弗朗索瓦·Savard建议,下面是你会得到什么样的例子
你需要遍历ŧ他将它们标记并逐个添加到框架中。 – 2015-02-09 14:58:42
如果你想让他们并排,那么http://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html可能会让你感兴趣。 – 2015-02-09 14:59:26