標題:
滑鼠事件 (三)
[打印本頁]
作者:
tonyh
時間:
2016-5-28 17:34
標題:
滑鼠事件 (三)
本帖最後由 tonyh 於 2019-6-22 12:58 編輯
實作 MouseListener 與 MouseMotionListener 介面, 以完成滑鼠指標拖曳圖形之動作.
JFrame的設定: 100, 100, 420, 320
JLabel的設定: x, y, 128, 128
本帖隱藏的內容需要回復才可以瀏覽
作者:
張峻瑋
時間:
2016-6-8 20:43
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch100 implements MouseListener,MouseMotionListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon_s,icon;
private int x=100,y=100,x1,y1,x2,y2;
private boolean isDrag=false;
Ch100()
{
icon_s=new ImageIcon(Ch100.class.getResource("pic/star.png"));
icon=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
lb=new JLabel(icon);
lb.setBounds(0,0,128,128);
lb.addMouseListener(this);
lb.addMouseMotionListener(this);
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setIconImage(icon_s.getImage());
fm.setBounds(100,100,420,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
}
public void mousePressed(MouseEvent e){
if(isDrag)
return;
if(e.getButton()==1)
isDrag=true;
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){
if(!isDrag)
return;
isDrag=false;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
if(!isDrag)
return;
x2=e.getX();
y2=e.getY();
x+=(x2-x1);
y+=(y2-y1);
lb.setLocation(x, y);
}
public static void main(String[] args){
new Ch100();
}
}
複製代碼
作者:
劉得恩
時間:
2016-6-10 21:42
本帖最後由 劉得恩 於 2016-6-18 16:58 編輯
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch100 implements MouseListener,MouseMotionListener
{
private JFrame f;
private JLabel s;
private ImageIcon icon1,icon2;
private boolean isDrag=false;
int x=100,y=100,x1,y1,x2,y2;
Ch100()
{
icon1=new ImageIcon(Ch100.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
s=new JLabel(icon2);
s.setBounds(x,y,128,128);
s.addMouseListener(this);
s.addMouseMotionListener(this);
f=new JFrame("\u3043\u0a9c\uc0ee");
f.setBounds(100,150,500,400);
f.setIconImage(icon1.getImage());
f.setVisible(true);
f.setResizable(false);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(s);
}
public static void main(String args[])
{
new Ch100();
}
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){
}
public void mouseClicked(MouseEvent e){}
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
x+=x2-x1;
y+=y2-y1;
s.setLocation(x,y);
}
public void mouseEntered(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
複製代碼
作者:
張峻瑋
時間:
2016-6-18 16:57
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch100 implements MouseListener,MouseMotionListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon_s,icon;
private int x=100,y=100,x1,y1,x2,y2;
Ch100()
{
icon_s=new ImageIcon(Ch100.class.getResource("pic/star.png"));
icon=new ImageIcon(Ch100.class.getResource("pic/santa.png"));
lb=new JLabel(icon);
lb.setBounds(0,0,128,128);
lb.addMouseListener(this);
lb.addMouseMotionListener(this);
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setIconImage(icon_s.getImage());
fm.setBounds(100,100,420,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
}
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
x+=(x2-x1);
y+=(y2-y1);
lb.setLocation(x, y);
}
public static void main(String[] args){
new Ch100();
}
}
複製代碼
作者:
林宇翔
時間:
2016-6-18 17:02
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch96 implements MouseListener,MouseMotionListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon_s,icon;
private int x=100,y=100,x1,y1,x2,y2;
private boolean isDrag=false;
Ch96()
{
icon_s=new ImageIcon(Ch96.class.getResource("pic/star.png"));
icon=new ImageIcon(Ch96.class.getResource("pic/santa.png"));
lb=new JLabel(icon);
lb.setBounds(0,0,128,128);
lb.addMouseListener(this);
lb.addMouseMotionListener(this);
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setIconImage(icon_s.getImage());
fm.setBounds(100,100,420,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
}
public void mousePressed(MouseEvent e){
if(isDrag)
return;
if(e.getButton()==1)
isDrag=true;
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){
if(!isDrag)
return;
isDrag=false;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e)
{
if(!isDrag)
return;
x2=e.getX();
y2=e.getY();
x+=(x2-x1);
y+=(y2-y1);
lb.setLocation(x, y);
}
public static void main(String[] args){
new Ch96();
}
}
複製代碼
作者:
李允軒
時間:
2016-6-18 17:03
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch01 implements MouseListener,MouseMotionListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon1,icon2;
private int x=100,y=100,x1,y1,x2,y2;
Ch01()
{
icon1=new ImageIcon(Ch01.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch01.class.getResource("pic/santa.png"));
lb=new JLabel(icon2);
lb.setBounds(x,y,128,128);
lb.addMouseListener(this);
lb.addMouseMotionListener(this);
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setIconImage(icon1.getImage());
fm.setBounds(100,100,420,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
}
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
x+=(x2-x1);
y+=(y2-y1);
lb.setLocation(x, y);
}
public static void main(String[] args){
Ch01 app=new Ch01();
}
}
複製代碼
作者:
張彥承
時間:
2016-6-18 17:08
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch96 implements MouseListener,MouseMotionListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon_s,icon;
private int x=100,y=100,x1,y1,x2,y2;
private boolean isDrag=false;
Ch96()
{
icon_s=new ImageIcon(Ch96.class.getResource("pic/star.png"));
icon=new ImageIcon(Ch96.class.getResource("pic/santa.png"));
lb=new JLabel(icon);
lb.setBounds(0,0,128,128);
lb.addMouseListener(this);
lb.addMouseMotionListener(this);
fm=new JFrame("滑鼠指標拖曳圖形");
fm.setIconImage(icon_s.getImage());
fm.setBounds(100,100,420,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
}
public void mousePressed(MouseEvent e){
if(isDrag)
return;
if(e.getButton()==1)
isDrag=true;
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){
if(!isDrag)
return;
isDrag=false;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e)
{
if(!isDrag)
return;
x2=e.getX();
y2=e.getY();
x+=(x2-x1);
y+=(y2-y1);
lb.setLocation(x, y);
}
public static void main(String[] args){
new Ch96();
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2