改变鼠标的状态,显示为忙碌

主要步骤是先创建自定义的鼠标指针,然后利用JFrame的setCursor方法设置鼠标


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class AnimatedCursor implements Runnable, ActionListener {

    private boolean animate;

    private Cursor[] cursors;

    private JFrame frame;

    public AnimatedCursor(JFrame frame) {

        animate = false;        

        cursors = new Cursor[8];

        this.frame = frame;

        cursors[0] = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);

        cursors[1] = Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);

        cursors[2] = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);

        cursors[3] = Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);

        cursors[4] = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);

        cursors[5] = Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);

        cursors[6] = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);

        cursors[7] = Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);

    }


    public void run() {

        int count = 0;

        while(animate) {

            try {

                Thread.currentThread().sleep(200);

            } catch (Exception ex) {

            }

            

            frame.setCursor(cursors[count % cursors.length]);

            count++;

        }

        frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

    }


    public void actionPerformed(ActionEvent evt) {

        JButton button = (JButton)evt.getSource();

        if(animate) {

            button.setText("Start Animation");

            animate = false;

        } else {

            animate = true;

            button.setText("Stop Animation");

            new Thread(this).start();

        }

    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("Animated Cursor Hack");


        JButton button = new JButton("Start Animation");

        button.addActionListener(new AnimatedCursor(frame));


        frame.getContentPane().add(button);

        frame.pack();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

    

    public static void p(String str) {

        System.out.println(str);

    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值