返回列表 發帖
  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.ImageIcon;
  4. import java.awt.event.MouseListener;
  5. import java.awt.event.MouseEvent;

  6. public class ch98 implements MouseListener{
  7.         
  8.     private JFrame fm;
  9.     private JLabel lb;
  10.     private ImageIcon icon,target;
  11.     private int x=100,y=100;
  12.         
  13.     ch98()
  14.     {
  15.         icon=new ImageIcon(ch98.class.getResource("pic/star.png"));
  16.         target=new ImageIcon(ch98.class.getResource("pic/santa.png"));
  17.                
  18.         lb=new JLabel(target);
  19.         lb.setBounds(x, y, 128, 128);
  20.                
  21.         fm=new JFrame("滑鼠指標牽引圖形");
  22.         fm.setBounds(100, 100, 420, 320);
  23.         fm.setIconImage(icon.getImage());
  24.         fm.setVisible(true);
  25.         fm.setResizable(false);
  26.         fm.setLayout(null);
  27.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         fm.add(lb);
  29.         fm.addMouseListener(this);
  30.     }
  31.         
  32.     public void mousePressed(MouseEvent e)
  33.     {
  34.             if(e.getClickCount()==1)
  35.             {
  36.                     x=e.getX()-3;
  37.                     y=e.getY()-25;
  38.         }
  39.             if(e.getClickCount()==2)
  40.             {
  41.                     x=e.getX()-3-64;
  42.                     y=e.getY()-25-64;
  43.         }
  44.             lb.setLocation(x,y);
  45.     }
  46.     public void mouseReleased(MouseEvent e){}
  47.    
  48.     public void mouseClicked(MouseEvent e){}
  49.         
  50.     public void mouseEntered(MouseEvent e){}
  51.         
  52.     public void mouseExited(MouseEvent e){}
  53.     public static void main(String[] args) {
  54.         new ch98();
  55.     }

  56. }
複製代碼

TOP

返回列表