python打地鼠鼠标控制锤子_求助,swing打地鼠游戏,怎么把鼠标光标能换成一个锤子的图片 - 网络管理论坛 - 51CTO技术论坛_中国领先的IT技术社区...

package Shrewmouse;

import java.awt.Cursor;

import java.awt.EventQueue;

import java.awt.Image;

import java.awt.Point;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class Shrewmouse extends JFrame implements Runnable {

private JLabel[] mouses; // 存放显示底数的标签数组

private ImageIcon img,imgMouse; // 地鼠图片对象

public static void main(String args[]) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Shrewmouse frame = new Shrewmouse(); // 创建窗体

frame.setVisible(true); // 显示窗体

new Thread(frame).start(); // 启动线程

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public Shrewmouse() {

super();

this.setResizable(false); // 禁止调整窗体大小

this.getContentPane().setLayout(null); // 窗体不使用布局管理器

this.setTitle("打地鼠"); // 设置窗体标题

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

img = new ImageIcon("E:/HJJ/youxi/bk.jpg");

// 初始化背景图片

imgMouse = new ImageIcon("E:/HJJ/youxi/mouse.png");

// 初始化地鼠图片对象

mouses = new JLabel[9]; // 创建显示地鼠的标签数组

for (int i = 0; i < 9; i++) { // 遍历数组

mouses[i] = new JLabel(); // 初始化每一个数组元素

mouses[i].setSize(imgMouse.getIconWidth(), imgMouse.getIconHeight());

// 设置标签与地鼠图片相同大小

mouses[i].addMouseListener(new MouseAdapter() {// 为标签添加鼠标事件监听适配器

/**

* 处理鼠标单击事件的方法

*/

@Override

public void mouseClicked(MouseEvent e) {

Object source = e.getSource(); // 获取事件源,即地鼠标签

if (source instanceof JLabel) { // 如果事件是标签组件

JLabel mouse = (JLabel) source; // 强制转换为JLabel标签

mouse.setIcon(null); // 取消标签图标

}

}

});

this.getContentPane().add(mouses[i]); // 添加显示地鼠的标签到窗体

}

mouses[0].setLocation(120, 310); // 设置每个标签的位置

mouses[1].setLocation(130, 210);

mouses[2].setLocation(160, 120);

mouses[3].setLocation(350, 310);

mouses[4].setLocation(350, 210);

mouses[5].setLocation(350, 120);

mouses[6].setLocation(570, 310);

mouses[7].setLocation(550, 210);

mouses[8].setLocation(540, 120);

final JLabel backLabel = new JLabel(); // 创建显示背景的标签

backLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());

this.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());

backLabel.setIcon(img); // 添加背景到标签

this.getContentPane().add(backLabel); // 添加背景标签到窗体

}

/**

* 线程的核心方法

*/

public void run() {

while (true) { // 使用无限循环

try {

Thread.sleep(2000); // 使线程休眠2秒

int index = (int) (Math.random() * 9); // 生成随机的地鼠索引

if (mouses[index].getIcon() == null) { // 如果地鼠标签没有设置图片

mouses[index].setIcon(imgMouse); // 为该标签添加地鼠图片

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值