Results 1 to 6 of 6
  1. #1
    TP-Oreilly is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Moving an icon on screen?

    Hi, here is my code, I want to move the on screen icon by pressing a key but I dont know how to do it:

    Java Code:
    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*;
    
    class MoveIcon{
    
    	public static void main(String[]args){
    		//create window
    		JFrame frame = new JFrame("Moving Icon App");
    		frame.setSize(600,400);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    		
    		//create an image icon to add to a graphical component to add to the windows content pane
    		ImageIcon icon = new ImageIcon("dot.jpg");
    		
    		//create graphical component object to add to the windows content pane
    		JLabel label = new JLabel(icon);
    		
    		//get the windows content pane (object where graphical components go)
    		Container windowsContentPane = frame.getContentPane();
    		
    		//add the graphical object to the windows content pane
    		windowsContentPane.add(label);
    		
    		//set window visible
    		frame.setVisible(true);
    	
    	
    	
    	
    	
    	}
    
    
    
    
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Moving an icon on screen?

    A content pane has a LayoutManager installed and it's the LayoutManager that determines the position of the components, not you. Remove the LayoutManager (set it to null) before you add your JLabel to the content pane and move it around everywhere you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TP-Oreilly is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Moving an icon on screen?

    Can you tell me what line of code i would need to remove the LayoutManager?

  4. #4
    TP-Oreilly is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Moving an icon on screen?

    Ah i got it. You said "move it around everywhere you want", how would i do this?

  5. #5
    TP-Oreilly is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Moving an icon on screen?

    Also, ive used "frame.setLayout(null);" and my jlabel is now not appearing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Moving an icon on screen?

    Whenever you use a null layout, you, the programmer, take full responsibility for setting both the size and location of any components added to the null-using container. Suggestions:

    • Set the size of the JLabel (I often set it to its preferredSize via getPreferredSize())
    • Set the location of the JLabel, or if you do nothing, it will default to [0, 0]
    • Use a JPanel for the null layout-using container.
    • And add the JPanel to the JFrame (which should use its default layout), in the BorderLayout.CENTER position.
    • To move a component, you need to call setLocation(...) on it.

Similar Threads

  1. Adding another icon to an icon
    By whateverme in forum New To Java
    Replies: 11
    Last Post: 03-29-2011, 08:00 PM
  2. Move from one screen fto other screen
    By jprgmr75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-02-2011, 06:32 PM
  3. Moving Cars across the screen, help!
    By Keno777 in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 02:57 PM
  4. How to make an icon in bottom-right of screen
    By hunterbdb in forum AWT / Swing
    Replies: 1
    Last Post: 08-13-2009, 08:28 AM
  5. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •