返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Ch98 implements MouseListener{
  4.         
  5.     private JFrame fm=new JFrame("滑鼠指標牽引圖形");;
  6.     private JLabel lb;
  7.     private ImageIcon icon,target;
  8.     private int x=100,y=100;
  9.         
  10.     Ch98()
  11.     {
  12.         icon=new ImageIcon(Ch98.class.getResource("pic/star.png"));
  13.         target=new ImageIcon(Ch98.class.getResource("pic/santa.png"));
  14.                
  15.         lb=new JLabel(target);
  16.         lb.setBounds(x, y, 128, 128);
  17.                
  18.         fm.setBounds(100, 100, 420, 320);
  19.         fm.setIconImage(icon.getImage());
  20.         fm.setVisible(true);
  21.         fm.setResizable(false);
  22.         fm.setLayout(null);
  23.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.         fm.add(lb);
  25.         fm.addMouseListener(this);
  26.     }
  27.         
  28.     public void mousePressed(MouseEvent e)
  29.     {
  30.         if(e.getClickCount()==1)
  31.         {
  32.             x=e.getX()-3;
  33.             y=e.getY()-25;
  34.         }
  35.         if(e.getClickCount()==2)
  36.         {
  37.             x=e.getX()-3-64;
  38.             y=e.getY()-25-64;
  39.         }
  40.         lb.setLocation(x, y);
  41.     }
  42.     public void mouseReleased(MouseEvent e){}
  43.         
  44.     public void mouseClicked(MouseEvent e){}
  45.         
  46.     public void mouseEntered(MouseEvent e){}
  47.         
  48.     public void mouseExited(MouseEvent e){}

  49.     public static void main(String[] args) {
  50.         new Ch98();
  51.     }

  52. }
複製代碼

TOP

返回列表