Results 1 to 3 of 3
- 04-06-2011, 02:22 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
How to randomly pick a JLabel that contain image
I try to create a function that can randomly pick a JLabel(image putting inside the label), however I don't know how to make it work.
I declare,
public class capt extends JFrame{
String [] capth = new String [10];
public capt(){
p2=new JPanel();
p2.setLayout(new GridLayout(1,1));
image =new JLabel();
image.setIcon(new ImageIcon("C:/Users/Kong/Pictures/Cap/Cap1.jpg"));
String [] prefix={"C:/Users/Kong/Pictures/Cap/Cap1.jpg"};
Random random= new Random();
for (int i = 0; i < capth.length; ++i)
{
capth[i] = prefix[random.nextInt(10)] ;
}
p2.add(image);
add(p2,BorderLayout.NORTH);
}
public static void main (String args[]){
capt c=new capt();
}
}
It seen like JLabel is not compatible with array.
Can anyone help me to solve the problem?Last edited by JJay84; 04-06-2011 at 03:20 PM.
- 04-06-2011, 02:49 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
What do you mean? Are there exceptions? Does it compile? I can say right now you've got an array of length 1 (prefix), and then try to access values from said array with indexes between 0 and 9...asking for an ArrayIndexOutOfBoundsException (and what you do with those arrays is not defined - an SSCCE would more clearly demonstrate the problem)I don't know how to make it work
- 04-06-2011, 03:15 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
It true, the program show an ArrayIndexOutOfBoundsException.
Exception in
capth[i] = prefix[random.nextInt(10)]
&
capt c=new capt();
But I think the problem is,
p2.add(image);
I want to add the result of the random image inside the JLabel.
It should be
p2.add(capth[i]);
However, it show that "cannot find symbol method add (java.lang.String)"Last edited by JJay84; 04-06-2011 at 03:35 PM.
Similar Threads
-
Displaying a selected image into a JLabel with JFileChooser when using a JmenuItem.
By Soufly in forum AWT / SwingReplies: 1Last Post: 03-19-2011, 11:06 PM -
Help: How to auto-resize the image icon to fit in the jlabel
By weikang in forum AWT / SwingReplies: 2Last Post: 11-24-2010, 02:38 PM -
Image On a JLabel
By corbokhan in forum New To JavaReplies: 11Last Post: 11-06-2010, 12:18 AM -
JLabel wont appear in front of background image
By Andolph23 in forum AWT / SwingReplies: 5Last Post: 05-27-2010, 01:42 AM -
Image in JLabel
By ishere11 in forum AWT / SwingReplies: 2Last Post: 04-29-2010, 08:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks