簡體   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