返回列表 發帖

滑鼠事件 (三)

本帖最後由 tonyh 於 2020-3-23 19:34 編輯

實作 MouseListener 與 MouseMotionListener 介面, 以完成滑鼠指標拖曳圖形之動作.
JFrame的設定: 100, 100, 420, 320
JLabel的設定: x, y, 128, 128





本帖隱藏的內容需要回復才可以瀏覽



呵呵,想酒家嗎?
今天酒家早就在瑞士逍遙了。
對了,你們這群八加九。
納猶泰茍.梁憨兒 & (千足百里十步一抓雞)西門洪振 & (那個衣服黃皮膚黃思想黃XX也黃的)姬安 & 可能還沒回來的易仔, 耕仔 & 一個好像沒存在感得被酒家忘記的某人,
要酒家買禮物的打在下面(不是打了然後X在下面)
當然假如酒家懶得看汝等在講啥幹話的話,
............... 就沒禮物了。

TOP

本帖最後由 洪振庭 於 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

import javax.swing.*;
import java.awt.event.*;

public class Ch100 implements MouseListener, MouseMotionListener{
        
        private JFrame fm;
        private JLabel lb;
        private ImageIcon icon, target;
        private int x=100, y=100, x1, y1, x2, y2;
        
        Ch100()
        {
                icon=new ImageIcon(Ch100.class.getResource("pic/star.png"));
                target=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
               
                lb=new JLabel(target);
                lb.setBounds(x, y, 128, 128);
                lb.addMouseListener(this);
                lb.addMouseMotionListener(this);
               
                fm=new JFrame("滑鼠指標拖曳圖形");
                fm.setBounds(100, 100, 420, 320);
                fm.setIconImage(icon.getImage());
                fm.setVisible(true);
                fm.setResizable(false);
                fm.setLayout(null);
                fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fm.add(lb);
        }
        
        public void mousePressed(MouseEvent e){
                x1=e.getX();
                y1=e.getY();
                //System.out.println(x1+" "+y1);
        }
        public void mouseReleased(MouseEvent e){}
        public void mouseClicked(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mouseDragged(MouseEvent e){
                x2=e.getX();
                y2=e.getY();
                x+=(x2-x1);
                y+=(y2-y1);
                lb.setLocation(x, y);
                //System.out.println(x2+" "+y2);
        }
        public void mouseMoved(MouseEvent e){}
        public static void main(String[] args) {
                new Ch100();
        }
}
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

  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 Ch100 implements MouseListener, MouseMotionListener{
  8.         
  9.         private JFrame fm;
  10.         private JLabel lb;
  11.         private ImageIcon icon, target;
  12.         private int x=100, y=100, x1, y1, x2, y2;
  13.         
  14.         Ch100()
  15.         {
  16.                 icon=new ImageIcon(Ch100.class.getResource("pic/star.png"));
  17.                 target=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
  18.                
  19.                 lb=new JLabel(target);
  20.                 lb.setBounds(x, y, 128, 128);
  21.                 lb.addMouseListener(this);
  22.                 lb.addMouseMotionListener(this);
  23.                
  24.                 fm=new JFrame("滑鼠指標拖曳圖形");
  25.                 fm.setBounds(100, 100, 420, 320);
  26.                 fm.setIconImage(icon.getImage());
  27.                 fm.setVisible(true);
  28.                 fm.setResizable(false);
  29.                 fm.setLayout(null);
  30.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.                 fm.add(lb);
  32.         }
  33.         
  34.         public void mousePressed(MouseEvent e){
  35.                 x1=e.getX();
  36.                 y1=e.getY();
  37.                 //System.out.println(x1+" "+y1);
  38.         }
  39.         public void mouseReleased(MouseEvent e){}
  40.         public void mouseClicked(MouseEvent e){}
  41.         public void mouseEntered(MouseEvent e){}
  42.         public void mouseExited(MouseEvent e){}
  43.         public void mouseDragged(MouseEvent e){
  44.                 x2=e.getX();
  45.                 y2=e.getY();
  46.                 x+=(x2-x1);
  47.                 y+=(y2-y1);
  48.                 lb.setLocation(x, y);
  49.                 //System.out.println(x2+" "+y2);
  50.         }
  51.         public void mouseMoved(MouseEvent e){}
  52.         public static void main(String[] args) {
  53.                 new Ch100();
  54.         }
  55. }
複製代碼

TOP

  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 Ch93 implements MouseListener
  7. {      
  8.     JFrame fm;
  9.     JLabel lb;
  10.     ImageIcon ic,ii1,ii2;
  11.     int x=100,y=100;
  12.    
  13.         Ch93()
  14.         {
  15.                 ic=new ImageIcon(Ch93.class.getResource("pic/star.png"));
  16.                 ii1=new ImageIcon(Ch93.class.getResource("pic/gift-green.png"));
  17.                 ii2=new ImageIcon(Ch93.class.getResource("pic/hippo.png"));
  18.                
  19.                 lb=new JLabel(ii1);
  20.                 lb.setBounds(x, y, 128, 128);
  21.                 lb.addMouseListener(this);        
  22.                
  23.                 fm=new JFrame("點擊切換圖片");
  24.                 fm.setBounds(100, 100, 420, 320);
  25.                 fm.setIconImage(ic.getImage());
  26.                 fm.setVisible(true);
  27.                 fm.setResizable(false);
  28.                 fm.setLayout(null);
  29.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.                 fm.add(lb);
  31.         }
  32.         
  33.         public void mousePressed(MouseEvent e)
  34.         {
  35.                 if(lb.getIcon()==ii1)
  36.                 {
  37.                     lb.setIcon(ii2);
  38.                 }
  39.                 else
  40.                         lb.setIcon(ii1);                        
  41.         }
  42.         public void mouseReleased(MouseEvent e){}        
  43.         public void mouseClicked(MouseEvent e){}        
  44.         public void mouseEntered(MouseEvent e){}
  45.         public void mouseExited(MouseEvent e){}
  46.         
  47.         public static void main(String[] args) {
  48.                 new Ch93();
  49.         }
  50. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

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

回復 2# 曾挺桂 我要我要

TOP

  1. import javax.swing.*;
  2. import java.awt.event.*;

  3. public class Ch100 implements MouseListener, MouseMotionListener{
  4.         
  5.         private JFrame fm;
  6.         private JLabel lb;
  7.         private ImageIcon icon, target;
  8.         private int x=100, y=100, x1, y1, x2, y2;
  9.         
  10.         Ch100()
  11.         {
  12.                 icon=new ImageIcon(Ch100.class.getResource("pic/star.png"));
  13.                 target=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
  14.                
  15.                 lb=new JLabel(target);
  16.                 lb.setBounds(x, y, 128, 128);
  17.                 lb.addMouseListener(this);
  18.                 lb.addMouseMotionListener(this);
  19.                
  20.                 fm=new JFrame("滑鼠指標拖曳圖形");
  21.                 fm.setBounds(100, 100, 420, 320);
  22.                 fm.setIconImage(icon.getImage());
  23.                 fm.setVisible(true);
  24.                 fm.setResizable(false);
  25.                 fm.setLayout(null);
  26.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.                 fm.add(lb);
  28.         }
  29.         
  30.         public void mousePressed(MouseEvent e){
  31.                 x1=e.getX();
  32.                 y1=e.getY();
  33.                 //System.out.println(x1+" "+y1);
  34.         }
  35.         public void mouseReleased(MouseEvent e){}
  36.         public void mouseClicked(MouseEvent e){}
  37.         public void mouseEntered(MouseEvent e){}
  38.         public void mouseExited(MouseEvent e){}
  39.         public void mouseDragged(MouseEvent e){
  40.                 x2=e.getX();
  41.                 y2=e.getY();
  42.                 x+=(x2-x1);
  43.                 y+=(y2-y1);
  44.                 lb.setLocation(x, y);
  45.                 //System.out.println(x2+" "+y2);
  46.         }
  47.         public void mouseMoved(MouseEvent e){}
  48.         public static void main(String[] args) {
  49.                 new Ch100();
  50.         }
  51. }
複製代碼

TOP

呵呵,想酒家嗎?
今天酒家早就在瑞士逍遙了。
對了,你們這群八加九。
納猶泰茍.梁憨兒 & (千足百里十步一 ...
曾挺桂 發表於 2017-6-29 08:33

回復 2# 曾挺桂


    我也要

TOP

回復  2# 曾挺桂

有我的份!!
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

  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 Ch100 implements MouseListener, MouseMotionListener{
  8.         
  9.         private JFrame fm;
  10.         private JLabel lb;
  11.         private ImageIcon icon, target;
  12.         private int x=100, y=100, x1, y1, x2, y2;
  13.         
  14.         Ch100()
  15.         {
  16.                 icon=new ImageIcon(Ch100.class.getResource("pic/star.png"));
  17.                 target=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
  18.                
  19.                 lb=new JLabel(target);
  20.                 lb.setBounds(x, y, 128, 128);
  21.                 lb.addMouseListener(this);
  22.                 lb.addMouseMotionListener(this);
  23.                
  24.                 fm=new JFrame("滑鼠指標拖曳圖形");
  25.                 fm.setBounds(100, 100, 420, 320);
  26.                 fm.setIconImage(icon.getImage());
  27.                 fm.setVisible(true);
  28.                 fm.setResizable(false);
  29.                 fm.setLayout(null);
  30.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.                 fm.add(lb);
  32.         }
  33.         
  34.         public void mousePressed(MouseEvent e){
  35.                 x1=e.getX();
  36.                 y1=e.getY();
  37.                 //System.out.println(x1+" "+y1);
  38.         }
  39.         public void mouseReleased(MouseEvent e){}
  40.         public void mouseClicked(MouseEvent e){}
  41.         public void mouseEntered(MouseEvent e){}
  42.         public void mouseExited(MouseEvent e){}
  43.         public void mouseDragged(MouseEvent e){
  44.                 x2=e.getX();
  45.                 y2=e.getY();
  46.                 x+=(x2-x1);
  47.                 y+=(y2-y1);
  48.                 lb.setLocation(x, y);
  49.                 //System.out.println(x2+" "+y2);
  50.         }
  51.         public void mouseMoved(MouseEvent e){}
  52.         public static void main(String[] args) {
  53.                 new Ch100();
  54.         }
  55. }
複製代碼

TOP

返回列表