標題:
滑鼠事件 (二)
[打印本頁]
作者:
tonyh
時間:
2017-6-23 20:41
標題:
滑鼠事件 (二)
本帖最後由 tonyh 於 2019-5-25 14:22 編輯
實作指標位置對準圖片, 點擊滑鼠左鍵切換圖片.
本帖隱藏的內容需要回復才可以瀏覽
作者:
洪振庭
時間:
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.MouseEvent;
public class Ch99 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,target,gift;
private int n=1;
Ch99()
{
icon=new ImageIcon(Ch99.class.getResource("pic/star.png"));
target=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
gift=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
lb=new JLabel(target);
lb.setBounds(100, 100, 128, 128);
lb.addMouseListener(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){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(e.getButton()==1 && e.getClickCount()%2==1){
lb.setIcon(gift);
n++;
}
else if(e.getButton()==1 && e.getClickCount()%2==0){
lb.setIcon(target);
n++;
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch99();
}
}
複製代碼
作者:
陳思惟
時間:
2017-6-30 19:59
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
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:41
import javax.swing.*;
import java.awt.event.*;
public class Ch99 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,target,gift;
private int n=1;
Ch99()
{
icon=new ImageIcon(Ch99.class.getResource("pic/star.png"));
target=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
gift=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
lb=new JLabel(target);
lb.setBounds(100, 100, 128, 128);
lb.addMouseListener(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){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(e.getButton()==1 && e.getClickCount()%2==1){
lb.setIcon(gift);
n++;
}
else if(e.getButton()==1 && e.getClickCount()%2==0){
lb.setIcon(target);
n++;
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch99();
}
}
複製代碼
作者:
黃璽安
時間:
2017-6-30 20:41
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-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.MouseEvent;
public class Ch99 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,target,gift;
private int n=1;
Ch99()
{
icon=new ImageIcon(Ch99.class.getResource("pic/star.png"));
target=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
gift=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
lb=new JLabel(target);
lb.setBounds(100, 100, 128, 128);
lb.addMouseListener(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){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(e.getButton()==1 && e.getClickCount()%2==1){
lb.setIcon(gift);
n++;
}
else if(e.getButton()==1 && e.getClickCount()%2==0){
lb.setIcon(target);
n++;
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch99();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2