Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Bringing an image in front of another

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Thanked 0 Times in 0 Posts

    Default Bringing an image in front of another

    import javax.swing.*;
    import java.awt.*;
    public class ImageTutorial extends JFrame	{
    		private ImageIcon image;
    		private JLabel label;
    		private ImageIcon mario;
    		private JLabel mariolabel;
    		private ImageIcon luige;
    		private JLabel luigelabel;
    		ImageTutorial()	{
    		    setLayout(new GridBagLayout());
    			image = new ImageIcon(getClass().getResource("main/castle.png"));
    			label = new JLabel(image);
    			mario = new ImageIcon(getClass().getResource("main/mario.jpg"));
    			mariolabel = new JLabel(mario);
    			luige = new ImageIcon(getClass().getResource("main/luige.jpg"));
    			luigelabel = new JLabel(luige);
    		public static void main(String [] args)
    			ImageTutorial gui = new ImageTutorial();
    			gui.setTitle("Mario vs Luige Quiz Board Game");

    How do I make it so that my luigelabel and mariolabel are infront of my label. Currently i can only see my label (background image) :S

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    My Mood
    Thanked 263 Times in 232 Posts

    Default Re: Bringing an image in front of another

    A label does not work the way it appears you are trying to make it work. The add method comes from the Component class up the hierarchy and is intended for other uses.
    (I now make the assumption that you intend to move mario and luige around):
    I think what would work better is some custom painting. This tutorial on custom painting should help.

Similar Threads

  1. vertical, front to back and string into decimal
    By arl13i1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2013, 07:15 PM
  2. Trying to create a GUI front end for a TELNET application
    By arkroan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 7th, 2013, 07:43 PM
  3. How to place a number in front of a string?!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 9th, 2011, 12:15 AM
  4. Stupid thing can't find image icon right in front of it!!!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2010, 02:02 AM