java中怎么让图片随鼠标移动呢?
就是我按住图片然后移动鼠标图片就跟着移动,当取消单击时鼠标也停。就是说我可以布置页面里的一些图片的位置。望高手指教!谢谢请问能解释一下我怎么运行吗?编译出class文件后...
就是我按住图片然后移动鼠标图片就跟着移动,当取消单击时鼠标也停。就是说我可以布置页面里的一些图片的位置。望高手指教!谢谢
请问能解释一下我怎么运行吗?编译出class文件后怎么运行啊? 它产生好几个class文件,我该怎么看结果啊?我是新手,请帮帮忙吧 展开
请问能解释一下我怎么运行吗?编译出class文件后怎么运行啊? 它产生好几个class文件,我该怎么看结果啊?我是新手,请帮帮忙吧 展开
2个回答
展开全部
汗!怎么运行你都搞不倒吗??!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-06-16
展开全部
//给个实例
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;
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
//图标
Icon icon = new ImageIcon(new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));
// 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();
}
});
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) {}
});
}
}
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;
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
//图标
Icon icon = new ImageIcon(new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));
// 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();
}
});
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) {}
});
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询