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类型
  • 热门搜索
    奥运会什么时候 hard是什么意思 菩萨蛮大柏地的意思 oil是什么意思 国色天香的意思 崎岖的意思 什么泡脚最好 o2o是什么意思 雄伟的意思 造访的意思