標題:
滑鼠事件 (二)
[打印本頁]
作者:
tonyh
時間:
2016-5-28 16:28
標題:
滑鼠事件 (二)
本帖最後由 tonyh 於 2017-6-23 20:39 編輯
實作指標位置對準圖片, 點擊滑鼠左鍵切換圖片.
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 icon1, icon2, icon3;
Ch99()
{
icon1=new ImageIcon(Ch99.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
icon3=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
lb=new JLabel(icon2);
lb.setBounds(100, 100, 128, 128);
lb.addMouseListener(this);
fm=new JFrame("點按滑鼠切換圖形");
fm.setBounds(100, 100, 420, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
}
public void mousePressed(MouseEvent e)
{
if(e.getButton()==1)
{
if(lb.getIcon()==icon2)
lb.setIcon(icon3);
else
lb.setIcon(icon2);
}
}
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 Ch99();
}
}
複製代碼
作者:
李允軒
時間:
2016-5-28 17:22
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,icon2,icon3;
private int x=100,y=100;
Ch99()
{
icon=new ImageIcon(Ch99.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
icon3=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 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){
if(lb.getIcon()==icon2)
lb.setIcon(icon3);
else
lb.setIcon(icon2);
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
new Ch99();
}
}
複製代碼
作者:
張峻瑋
時間:
2016-5-28 17:24
import javax.swing.*;
import java.awt.event.*;
public class Ch99 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon1,icon2,icon3;
Ch99()
{
icon1=new ImageIcon(Ch99.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
icon3=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
lb=new JLabel(icon3);
lb.setBounds(100, 100, 128, 128);
lb.addMouseListener(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)
{
if(e.getButton()==1)
{
if(lb.getIcon()==icon3)
lb.setIcon(icon2);
else
lb.setIcon(icon3);
}
}
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 Ch99();
}
}
複製代碼
作者:
劉得恩
時間:
2016-5-28 17:24
import javax.swing.*;
import java.awt.event.*;
public class Ch99 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon1,icon2,icon3;
Ch99(){
fm=new JFrame ("\u0a0f\u3450");
fm.setLayout(null);
fm.setResizable(false);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setBounds(100,100,500,500);
icon1=new ImageIcon(Ch99.class.getResource("pic/star.png"));
icon2=new ImageIcon(Ch99.class.getResource("pic/gift-green.png"));
icon3=new ImageIcon(Ch99.class.getResource("pic/hippo.png"));
fm.setIconImage(icon1.getImage());
lb=new JLabel(icon2);
lb.setBounds(200,200,128,128);
lb.addMouseListener(this);
fm.add(lb);
}
public static void main(String[] args) {
new Ch99();
}
public void mousePressed(MouseEvent e){
if(e.getButton()==1)
{
if(lb.getIcon()==icon2)
lb.setIcon(icon3);
else
lb.setIcon(icon2);
}
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
複製代碼
作者:
林宇翔
時間:
2016-5-28 17:26
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class ch98 implements MouseListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon,icon1,icon2;
private int x=100,y=100;
ch98()
{
icon=new ImageIcon(ch98.class.getResource("pic/star.png"));
icon1=new ImageIcon(ch98.class.getResource("pic/gift-green.png"));
icon2=new ImageIcon(ch98.class.getResource("pic/hippo.png"));
lb=new JLabel(icon1);
lb.setBounds(x, y, 128, 128);
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);
fm.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()%2==1)
lb.setIcon(icon2);
if(e.getClickCount()%2==0)
lb.setIcon(icon1);
}
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 ch98();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2