2015-04-23 67 views
-3

球員我想知道是否是一種方式來放置例如JLabels的列表,並用方法隨機選擇其中一個。從列表中隨機選擇一個對象(圖像)

如果JLabels是圖像,它仍然會工作?

感謝您的答案;)

+1

的[隨機從列表中選擇一個項目]可能重複(http://stackoverflow.com/questions/12487592/從列表中隨機選擇一個項目) –

+0

[從一組中挑選一個隨機元素]的可能重複(http://stackoverflow.com/questions/124671/picking-a-random-element-from-一套) –

回答

0

是的,它是可能的:

Random r = new Random(); 
     List<JLabel> yourList; //populate your list beforehand. yourList.add(JLabel blah); and so on 
     JLabel randomJLabel = yourList.get(r.nextInt(yourList.size()));