- import javax.swing.*;
- public class Ch87 {
- public static void main(String[] args) {
- private JFrame fm;
- private JLabel lb;
- private ImageIcon icon,target;
- private int x=100, y=100;
-
- Ch87()
- {
- icon=new ImageIcon(Ch87.class.getResource("pic/star.png"));
- target=new ImageIcon(Ch87.class.getResource("pic/star.png"));
-
- lb=new JLabel(target);
- 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)()==1)
- {
- x=e.getX()-3;
- y=e.getY()-25;
- }
- if(e.getClickCount)()==1)
- {
- x=e.getX()-3-64;
- y=e.getY()-25-64;
- }
- lb.setLocation(x, y);
- }
- public void mouseRleased(mouseEvent e){}
-
- public void mouseClicked(mouseEvent e){}
-
- public void mouseEntered(mouseEvent e){}
-
- public void mouseExcited(mouseEvent e){}
-
- public static void main(String[] args){
- Ch87 app=new Ch87();
- }
- }
複製代碼 |