Search code examples
javaswingjlabellayout-managernull-layout-manager

ImageIcon by JLabel example with absolute positioning


I have seen a lot of example code but none that specifically shows me how to display an ImageIcon through the use of a JLabel on a JFrame without using a layout manager. I am repeatedly told that absolute positioning is a hassle and less efficient than a layout manager but I'd prefer complete control over where my elements are going. A full scale example class (with no extra things added) of a JLabel that will display an image on a JFrame with absolute positioning would be much appreciated, thank you


Solution

  • The image I need has to move around the screen

    An important piece of information missing in the original question. I don't have a problem using a null layout for this.

    I wanted to move around with arrow keys on the screen?

    And don't use a KeyListener for this. Instead you should be using Key Bindings.

    See the KeyboardAnimation.java code found in Motion Using the Keyboard for one solution to this problem.