java怎么设置图片跟随鼠标

 时间:2024-10-21 06:37:28

1、//给个实例import java.awt.Color;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.net.URL;import javax.swing.BorderFactory;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;

java怎么设置图片跟随鼠标

2、public class ImageMove {static int x,y;public static void main(String[] args) throws Exception{JFrame f = new JFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.getContentPane().setLayout(null);//这个要设置成 null

java怎么设置图片跟随鼠标

3、//图标Iconicon = new ImageIcon(newURL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));

java怎么设置图片跟随鼠标

4、// Icon icon = new ImageIcon("c:/logo-zhidao.gif");//本地图片文件JLabel l = new JLabel(icon);l.setSize(icon.getIconWidth(),icon.getIconHeight());l.setBorder(BorderFactory.createLineBorder(Color.red));f.getContentPane().add(l);f.setVisible(true);l.addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e){x=e.getX();y=e.getY();}});

java怎么设置图片跟随鼠标

5、l.addMouseMotionListener(new MouseMotionListener(){public void mouseDragged(MouseEvent e) {JLabel l = (JLabel)e.getSource();l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);}public void mouseMoved(MouseEvent e) {}});}}

java怎么设置图片跟随鼠标
  • Intellij IDEA中如何配置Tomcat服务器?
  • 如何定义div位置
  • jquery如何将span标签内容的颜色改变
  • pycharm怎么调背景颜色
  • js如何改变input的type类型
  • 热门搜索
    大连交通大学怎么样 琴书大全 新年画画图片大全 刘思齐简介 海贼王图片大全 谍战剧大全 舞蹈大全 qq情侣头像大全 舞蹈视频大全 白头发是怎么回事