簡體   English   中英

在java中向圖像或圖像圖標添加鼠標偵聽器

[英]Adding a mouse listener to image or image Icon in java

如何在 Java 中向 im​​age 或imageIcon添加鼠標偵聽器?

這是我的代碼。 鼠標單擊后,我想對imageIcon執行任何操作。

public class Bubbles extends JPanel {
    
    public static final int LINE_OUT = 440;
    int x = 20;
    int y = 50;
    int v = 4;
    
    Image img = new ImageIcon("res/lop.png").getImage();
    BackSide back;
    

    public Bubbles(int x, int y, int v, BackSide back) {
        this.x = x;
        this.y = y;
        this.v = v;
        this.back = back;
        setFocusable(true);
        
    }
    
 
    public void move() {
        if (y >= 440) {
            y = 0;
        } else {
            y += v;
        }
    }

}

您可以做的是,您可以使用JLabel並將ImageIcon設置為它。 使用setOpaque(false)使JLabel透明。 然后,向它添加偵聽器並做任何您想做的事情:)

如果您不想使用 JLabel,您可以通過覆蓋paintComponent 方法將圖像繪制到面板上。然后您可以向您的 JPanel 添加偵聽器。我強烈建議您使用 JLabel 但如果您仍然喜歡其他方式,在這里您可以看到一些例如如何做到這一點。 如何在Java中設置背景圖像? .

Icon icon = new ImageIcon("give path of the image");
JLabel lb = new JLabel(icon);
//now set listener for label.
lb.addMouseListener(new MouseAdapter() 
{
    @Override
    public void mouseClicked(MouseEvent e) 
    {
        //statement             
    }
});

最簡單的解決方案是使用JLabel並設置它的icon屬性。 有關更多詳細信息,請參閱如何使用標簽

如果您必須自己繪制圖像(即,您想要跨容器應用效果或執行動畫),那么您需要將MouseListener添加到正在渲染圖像的容器中,並測試鼠標事件是否發生在上下文中圖像的位置和大小。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ImageMouseListener {

    public static void main(String[] args) {
        new ImageMouseListener();
    }

    public ImageMouseListener() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ImagePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage img;
        private Point imgPoint;

        public ImagePane() {
            try {
                img = ImageIO.read(...);
                imgPoint = new Point(100, 100);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (img != null && imgPoint != null) {
                        Point me = e.getPoint();
                        Rectangle bounds = new Rectangle(imgPoint, new Dimension(img.getWidth(), img.getHeight()));
                        if (bounds.contains(me)) {
                            System.out.println("I was clicked!");
                        }
                    }
                }

            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null && imgPoint != null) {
                g.drawImage(img, imgPoint.x, imgPoint.y, this);
            }
        }

    }

}

查看執行自定義繪畫如何使用鼠標偵聽器了解更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM