概述
package com.efiles.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
/**
* @ClassName MouseController
* @Author Administrator
* @Date 2021/5/11 0011 上午 8:44
**/
public class MouseController implements Runnable {
private Robot robot;
private boolean isStop = false;
public MouseController() {
try {
ControllerFrame frame = new ControllerFrame("鼠标移动事件");
frame.setVisible(true);
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
@Override
public void run() {
int x;
int y;
Random random = new Random();
while (!isStop){
//随机生成坐标
x = random.nextInt(1000);
y = random.nextInt(1000);
//开始移动
robot.mouseMove(x, y);
//60s移动一次
robot.delay(1000 * 60);
}
}
/**
* GUI Frame 生成一个button,控制程序
*
* @author max
*/
private class ControllerFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JButton close = new JButton("close");
public ControllerFrame(String title) {
this();
setTitle(title);
}
public ControllerFrame() {
setLayout(new FlowLayout(FlowLayout.LEADING));
setSize(316, 338);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Dimension preferredSize = new Dimension(300, 300);
Font font = new Font("", 1, 80);
//设置button 大小,文字等属性
close.setPreferredSize(preferredSize);
close.setFont(font);
close.setBorderPainted(true);
close.setFocusable(false);
add(close);
//点击button后,程序终止。
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isStop = true;
dispose();
}
});
}
}
public static void main(String[] args) {
MouseController controller = new MouseController();
controller.run();
}
}
最后
以上就是沉默短靴为你收集整理的java鼠标移动的全部内容,希望文章能够帮你解决java鼠标移动所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复