Vaibhav Setia
Vaibhav Setia

Reputation: 35

Jlabel not moving with mouse motion listener

I'm trying to implement a JLabel which moves with mouse pointer in a container with mouseMotionListener, but the JLabel does not appear on the screen. Any suggestions?

public class Ships extends JFrame implements ActionListener{

    private JPanel contentPane;

    int x=418,p=75,l=10;


    public static void main(String[] args) {
        Ships m = new Ships();

    }

    JLabel lblNewLabel = new JLabel();

    JLabel l5 = new JLabel();

    Container container;
    /**
    * Create the frame.
    */
    public Ships() {
         Container container= getContentPane();


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(0, 0, 1363, 730);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setVisible(true);

        l5.setIcon(new ImageIcon("C:\\Users\\The Wimpster\\Desktop\\images22.png"));
        container.add(l5);
        l5.setBounds(0, 10, 75, 50);//this label is supposed to move with mouse pointer
        container.addMouseMotionListener(new MouseAdapter(){ 
            public void mouseMoved(MouseEvent e){
                p = e.getX();
                l = e.getY();
                l5.setBounds(p,l,150,50); 
            }
        });
    }
}

Upvotes: 1

Views: 391

Answers (2)

user7627726
user7627726

Reputation:

Your code is very "fluffed". I have shortened it to only relevant parts and fixed it, and it looks like this:

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class Ships extends JFrame{
    private static final long serialVersionUID = 1L;
    private JLabel l5 = new JLabel();

    public Ships() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        l5.setIcon(new ImageIcon("C:\\Users\\The Wimpster\\Desktop\\images22.png"));
        add(l5);
        l5.setBounds(0, 10, 75, 50);
        addMouseMotionListener(new MouseAdapter(){ 
            public void mouseMoved(MouseEvent e){
                int x = e.getX();
                int y = e.getY();
                int imgWidth = 50; //change to width of image
                int imgHeight = 50; //change to height of image
                l5.setBounds(x-(imgWidth/2), y-(imgHeight/2), imgWidth, imgHeight); 
                l5.repaint();
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Ships();
            }
        });
    }
}

I made it as short as possible, removing all unnecessary variables, and other stuff. It works now, and gives a smoother output. The mouse is always in the center of the icon, once you change those 2 variables(I commented which ones).

Your main problem was that you were adding the Label to the wrong place, but I fixed that in my answer. Rather than adding it to a JPanel, I just added it directly to the JFrame, which is a lot simpler and produces the same effect

Upvotes: 1

Java Devil
Java Devil

Reputation: 10959

You are creating two instances of your Ships class - which the constructor calls setVisible(true) so two instances appear - this is probably not desired.

However the problem really comes about because you are getting the content pane of the form, then creating a new panel, setting that as the new content pane then you add the label to the old pane, then add the mouse listener to the old content pane.

Just remove the references to the existing pane and it'll work.

public Ships() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(0, 0, 1363, 730);
    contentPane = new JPanel();
    setContentPane(contentPane);
    contentPane.setLayout(null);
    setVisible(true);

    l5.setIcon(new ImageIcon("C:\\Users\\The Wimpster\\Desktop\\images22.png"));
    // 
    contentPane .add(l5);
    l5.setBounds(0, 10, 75, 50);//this label is supposed to move with mouse pointer
    contentPane .addMouseMotionListener(new MouseAdapter(){ 
        public void mouseMoved(MouseEvent e){
            p = e.getX();
            l = e.getY();
            l5.setBounds(p,l,150,50); 
        }
    }); 
}   

Upvotes: 2

Related Questions