package xuanfuchuang;
/**
* @author eggno8
* @version 1.0
*/
import java.awt.Toolkit;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.Timer;
//========================================
//FlyFly:漂浮flygarlic
//========================================
public class FlyFly extends JFrame implements java.awt.event.ActionListener {
private JButton jb = new JButton();
private Timer tm;
//-----最大点击次数,消失
private static int hp = 5;
//-----最大坐标
private static int W;
private static int H;
//-----当前坐标
private static int xp;
private static int yp;
//-----当前移动步径
private static int xstep = 3;
private static int ystep = 3;
//-----最大移动步径(随机范围,可以设置)
private static int maxxstep = 19;
private static int maxystep = 17;
public FlyFly(int w, int h) {
W = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - w;
H = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() - h;
System.out.println(W + ":" + H);
this.setSize(w, h);
xp = W / 2;
yp = H / 2;
this.setLocation(xp, yp);
this.setAlwaysOnTop(true); //窗体总在最前面
this.setResizable(false); //窗体不能改变大小
this.setUndecorated(true); //窗体不要边框
this.getContentPane().setLayout(new GridLayout(1, 1));
this.getContentPane().add(jb);
jb.setIcon(new ImageIcon("ds.jpg")); //同目录下的一张jpg图片,设置JButton的图片效果
jb.addActionListener(this); //!这个不能忘记,挂载ActionListener接口。
tm = new Timer(100, this); //!Timer是需要依附在一个实现了ActionListener接口的对象上。(单位毫秒,可以设置)
tm.start(); //!Timer需要打开
this.setVisible(true);
}
public void changeDirection() {
//如果到达边沿要转弯
if ( (xp <= 0 && xstep < 0) || (xp >= W && xstep > 0)) {
xstep = -xstep;
}
if ( (yp <= 0 && ystep < 0) || (yp >= H && ystep > 0)) {
ystep = -ystep;
}
this.setLocation(xp, yp); //位置重新设置
}
public void actionPerformed(ActionEvent e) {
//坐标按照当前状态移动
xp += xstep;
yp += ystep;
if (e.getSource() == jb) { //点击造成随机生成移动状态
hp--;
xp = (int) System.currentTimeMillis() % W;
yp = (int) System.currentTimeMillis() % H;
xstep = (int) System.currentTimeMillis() % (2 * maxxstep + 1) - maxxstep;
ystep = (int) System.currentTimeMillis() % (2 * maxystep + 1) - maxystep;
}
changeDirection(); //刷新显示状态
if (hp <= 0) {
System.exit(0);
}
}
public static void main(String[] _s) {
FlyFly ff = new FlyFly(64, 64);
}
}
java漂浮窗口源代码
2星 需积分: 45 24 浏览量
2015-02-25
21:54:10
上传
评论
收藏 9KB ZIP 举报
山有木兮卿有意_
- 粉丝: 39
- 资源: 1
最新资源
- F103-霸道开发板2.8寸电阻触摸屏例程.rar
- Google(高德)地图瓦片python代码下载
- Python实现输出杨辉三角形
- polsarpro官方教程、操作说明 PolSARpro v5.0 Software Training Course
- STM32 TouchGFX的使用二图片显示
- buildx镜像文件,也可以通过网上其他方式获取
- 【中级软件设计师】上午题12-软件工程(2):单元测试、黑盒测试、白盒测试、软件运行与维护
- 免费计算机毕业设计-医院住院管理系统的设计与实现(包含代码+论文)
- tt100k数据转换yolo格式
- 免费计算机毕业设计-学生在线网络考试系统的设计与实现(包含论文+源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论1