繁体   English   中英

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

[英]Jlabel not moving with mouse motion listener

我试图实现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); 
            }
        });
    }
}

您正在创建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); 
        }
    }); 
}   

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

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 ,这更简单并产生相同的效果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM