Results 1 to 6 of 6
Thread: Moving an icon on screen?
- 12-28-2011, 09:23 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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); } }
- 12-28-2011, 09:28 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-28-2011, 10:50 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: Moving an icon on screen?
Can you tell me what line of code i would need to remove the LayoutManager?
- 12-28-2011, 11:05 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: Moving an icon on screen?
Ah i got it. You said "move it around everywhere you want", how would i do this?
- 12-28-2011, 11:09 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: Moving an icon on screen?
Also, ive used "frame.setLayout(null);" and my jlabel is now not appearing.
-
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
-
Adding another icon to an icon
By whateverme in forum New To JavaReplies: 11Last Post: 03-29-2011, 08:00 PM -
Move from one screen fto other screen
By jprgmr75 in forum CLDC and MIDPReplies: 0Last Post: 01-02-2011, 06:32 PM -
Moving Cars across the screen, help!
By Keno777 in forum New To JavaReplies: 5Last Post: 03-04-2010, 02:57 PM -
How to make an icon in bottom-right of screen
By hunterbdb in forum AWT / SwingReplies: 1Last Post: 08-13-2009, 08:28 AM -
Blank Screen while navigating from one screen to another
By mohana.krishna in forum Java ServletReplies: 0Last Post: 03-03-2009, 05:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks