- import javax.swing.*;
- import java.awt.event.*;
- public class Ch98 implements MouseListener{
-
- private JFrame fm=new JFrame("滑鼠指標牽引圖形");;
- private JLabel lb;
- private ImageIcon icon,target;
- private int x=100,y=100;
-
- Ch98()
- {
- icon=new ImageIcon(Ch98.class.getResource("pic/star.png"));
- target=new ImageIcon(Ch98.class.getResource("pic/santa.png"));
-
- lb=new JLabel(target);
- lb.setBounds(x, y, 128, 128);
-
- 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()==1)
- {
- x=e.getX()-3;
- y=e.getY()-25;
- }
- if(e.getClickCount()==2)
- {
- x=e.getX()-3-64;
- y=e.getY()-25-64;
- }
- lb.setLocation(x, y);
- }
- 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();
- }
- }
複製代碼 |