返回列表 發帖
本帖最後由 洪振庭 於 2017-6-29 19:12 編輯
  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.MouseMotionListener;
  6. import java.awt.event.MouseEvent;
  7. public class Ch95 implements MouseListener,MouseMotionListener{
  8.          JFrame fm;
  9.          ImageIcon i1,icon;
  10.          JLabel lb;
  11.          int x=100,y=100,x1,y1,x2,y2;
  12.      Ch95()
  13.      {
  14.              i1=new ImageIcon(String.format("pic/santa.png"));     
  15.              icon=new ImageIcon(String.format("pic/star.png"));
  16.                  
  17.              lb=new JLabel(i1);
  18.              lb.setBounds( x, y, 128, 128);
  19.              lb.addMouseListener(this);
  20.              lb.addMouseMotionListener(this);
  21.             
  22.              fm=new JFrame("滑鼠事件 (三)");
  23.              fm.setBounds(100, 100, 420, 320);
  24.              fm.setVisible(true);
  25.              fm.setIconImage(icon.getImage());
  26.              fm.setResizable(false);
  27.              fm.setLayout(null);
  28.              fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.              fm.add(lb);      
  30.             
  31.      }
  32.      public void mousePressed(MouseEvent e){
  33.              x1=e.getX();
  34.          y1=e.getY();
  35.      }
  36.      public void mouseReleased(MouseEvent e){}   
  37.      public void mouseClicked(MouseEvent e){}
  38.      public void mouseEntered(MouseEvent e){}
  39.      public void mouseExited(MouseEvent e){}
  40.      public void mouseDragged(MouseEvent e){
  41.              x2=e.getX();
  42.          y2=e.getY();
  43.          x+=(x2-x1);
  44.          y+=(y2-y1);
  45.          lb.setLocation(x, y);
  46.      }
  47.      public void mouseMoved(MouseEvent e){}
  48.      public static void main(String[] args) {
  49.              new Ch95();
  50.      }

  51. }
複製代碼

TOP

本帖最後由 洪振庭 於 2017-6-30 20:34 編輯

回復 2# 曾挺桂 我要我要

TOP

返回列表