2015-02-09 110 views
-2

我想用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); 
+0

你需要遍歷ŧ他將它們標記並逐個添加到框架中。 – 2015-02-09 14:58:42

+4

如果你想讓他們並排,那麼http://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html可能會讓你感興趣。 – 2015-02-09 14:59:26

回答

2

您可以使用網格佈局

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建議,下面是你會得到什麼樣的例子

Grid Layout

相關問題