Jlabel没有用鼠标运动监听器移动

时间:2017-07-12 21:56:19

标签: java swing jframe mousemotionlistener

我正在尝试使用鼠标指针在JLabel的容器中实现mouseMotionListener,但JLabel不会出现在屏幕上。有什么建议吗?

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); 
            }
        });
    }
}

2 个答案:

答案 0 :(得分:2)

您正在创建Ships类的两个实例 - 构造函数调用setVisible(true),因此会出现两个实例 - 这可能不是所希望的。

然而问题确实来了,因为你正在获取表单的内容窗格,然后创建一个新面板,将其设置为新的内容窗格,然后将标签添加到旧窗格,然后将鼠标监听器添加到旧内容窗格。

只需删除对现有窗格的引用即可。

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); 
        }
    }); 
}   

答案 1 :(得分:1)

你的代码非常“松散”。我已将它缩短为仅相关部分并修复它,它看起来像这样:

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();
            }
        });
    }
}

我尽量缩短,删除所有不必要的变量和其他东西。它现在可以工作,并提供更平滑的输出。一旦你改变了那两个变量(我评论了哪些变量),鼠标总是在图标的中心。

你的主要问题是你将标签添加到错误的地方,但我在答案中修正了这个问题。我将其直接添加到JPanel,而不是将其添加到JFrame,这更简单并且产生相同的效果