標題:
滑鼠事件 (三)
[打印本頁]
作者:
tonyh
時間:
2017-6-23 21:14
標題:
滑鼠事件 (三)
本帖最後由 tonyh 於 2020-3-23 19:34 編輯
實作 MouseListener 與 MouseMotionListener 介面, 以完成滑鼠指標拖曳圖形之動作.
JFrame的設定: 100, 100, 420, 320
JLabel的設定: x, y, 128, 128
本帖隱藏的內容需要回復才可以瀏覽
作者:
曾挺桂
時間:
2017-6-29 08:33
呵呵,想酒家嗎?
今天酒家早就在瑞士逍遙了。
對了,你們這群八加九。
納猶泰茍.梁憨兒 & (千足百里十步一抓雞)西門洪振 & (那個衣服黃皮膚黃思想黃XX也黃的)姬安 & 可能還沒回來的易仔, 耕仔 & 一個好像沒存在感得被酒家忘記的某人,
要酒家買禮物的打在下面(不是打了然後X在下面)
當然假如酒家懶得看汝等在講啥幹話的話,
............... 就沒禮物了。
作者:
洪振庭
時間:
2017-6-29 19:05
本帖最後由 洪振庭 於 2017-6-29 19:12 編輯
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class Ch95 implements MouseListener,MouseMotionListener{
JFrame fm;
ImageIcon i1,icon;
JLabel lb;
int x=100,y=100,x1,y1,x2,y2;
Ch95()
{
i1=new ImageIcon(String.format("pic/santa.png"));
icon=new ImageIcon(String.format("pic/star.png"));
lb=new JLabel(i1);
lb.setBounds( x, y, 128, 128);
lb.addMouseListener(this);
lb.addMouseMotionListener(this);
fm=new JFrame("滑鼠事件 (三)");
fm.setBounds(100, 100, 420, 320);
fm.setVisible(true);
fm.setIconImage(icon.getImage());
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();
}
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);
}
public void mouseMoved(MouseEvent e){}
public static void main(String[] args) {
new Ch95();
}
}
複製代碼
作者:
梁和雋
時間:
2017-6-30 19:36
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();
}
}
作者:
陳思惟
時間:
2017-6-30 19:57
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
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, 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();
}
}
複製代碼
作者:
李知易
時間:
2017-6-30 20:29
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Ch93 implements MouseListener
{
JFrame fm;
JLabel lb;
ImageIcon ic,ii1,ii2;
int x=100,y=100;
Ch93()
{
ic=new ImageIcon(Ch93.class.getResource("pic/star.png"));
ii1=new ImageIcon(Ch93.class.getResource("pic/gift-green.png"));
ii2=new ImageIcon(Ch93.class.getResource("pic/hippo.png"));
lb=new JLabel(ii1);
lb.setBounds(x, y, 128, 128);
lb.addMouseListener(this);
fm=new JFrame("點擊切換圖片");
fm.setBounds(100, 100, 420, 320);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
}
public void mousePressed(MouseEvent e)
{
if(lb.getIcon()==ii1)
{
lb.setIcon(ii2);
}
else
lb.setIcon(ii1);
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch93();
}
}
複製代碼
作者:
洪振庭
時間:
2017-6-30 20:32
本帖最後由 洪振庭 於 2017-6-30 20:34 編輯
回復
2#
曾挺桂
我要我要
作者:
黃璽安
時間:
2017-6-30 20:42
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();
}
}
複製代碼
作者:
陳思惟
時間:
2017-6-30 21:07
呵呵,想酒家嗎?
今天酒家早就在瑞士逍遙了。
對了,你們這群八加九。
納猶泰茍.梁憨兒 & (千足百里十步一 ...
曾挺桂 發表於 2017-6-29 08:33
回復
2#
曾挺桂
我也要
作者:
李知易
時間:
2017-7-4 13:34
回復
2#
曾挺桂
有我的份!!
作者:
曾挺桂
時間:
2017-7-14 18:43
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
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, 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://istak.org.tw/seed/)
Powered by Discuz! 7.2