返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.*;

  3. public class Ch01 implements MouseListener{
  4.         
  5.     private JFrame fm;
  6.     private JLabel lb;
  7.     private ImageIcon icon,target;
  8.     private int x=100,y=100;
  9.         
  10.     Ch01()
  11.     {
  12.         icon=new ImageIcon(Ch01.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=new JFrame("滑鼠指標牽引圖形");
  19.         fm.setBounds(100, 100, 420, 320);
  20.         fm.setIconImage(icon.getImage());
  21.         fm.setVisible(true);
  22.         fm.setResizable(false);
  23.         fm.setLayout(null);
  24.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         fm.add(lb);
  26.         fm.addMouseListener(this);
  27.     }
  28.         
  29.     public void mousePressed(MouseEvent e)
  30.     {
  31.         if(e.getClickCount()==1)
  32.         {
  33.             x=e.getX()-3;
  34.             y=e.getY()-25;
  35.         }
  36.         if(e.getClickCount()==2)
  37.         {
  38.             x=e.getX()-3-64;
  39.             y=e.getY()-25-64;
  40.         }
  41.         lb.setLocation(x, y);
  42.     }
  43.     public void mouseReleased(MouseEvent e){}
  44.         
  45.     public void mouseClicked(MouseEvent e){}
  46.         
  47.     public void mouseEntered(MouseEvent e){}
  48.         
  49.     public void mouseExited(MouseEvent e){}

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

  53. }
複製代碼
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

返回列表