Hi there. If you are putting pictures in labels inside of a JFrame and there are 3 labels in the JFrame. You want the same picture for the first and second labels, but for the third label you want to change to a new picture.
How would you make it display the new picture ?
ie: I have done this :
and entered this above label3 in the code to try to display angels.gif
but I get the error:
LabelDemoGo.java:27: error: variable icon is already defined in method main (String[])
Heres the full code which works fine displaying all three labels with the same picture (But I want to make the third label have a different pictuere)
//LabelDemoGo.java //Demonstrates the use of image icons in labels. import java.awt.*; import javax.swing.*; public class LabelDemoGo { //creates and displays the primary application frame. public static void main (String[] args) { JFrame frame = new JFrame ("Label Demo"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); ImageIcon icon = new ImageIcon ("devil.gif"); JLabel label1, label2, label3; label1 = new JLabel ("TRY ", icon, SwingConstants.CENTER); label2 = new JLabel ("alittle ", icon, SwingConstants.CENTER); label2.setHorizontalTextPosition (SwingConstants.RIGHT); label2.setVerticalTextPosition (SwingConstants.CENTER); label3 = new JLabel ("HARDER -", icon, SwingConstants.CENTER); label3.setHorizontalTextPosition (SwingConstants.RIGHT); label3.setVerticalTextPosition (SwingConstants.CENTER); JPanel panel = new JPanel(); panel.setBackground (Color.pink); panel.setPreferredSize (new Dimension (250, 400)); panel.add (label1); panel.add (label2); panel.add (label3); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }
thank you.