package com.black;
import java.awt.AWTException;
public class dingshidianji extends JFrame {
private ButtonGroup bg;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private String fangshi = “InputEvent.BUTTON1_MASK”;// 默认方式
private int shijian = 0;
public int x_move = 0, y_move = 0;
static JLabel lblNewLabel_1;
private static String str;
private JLabel label_2;
/**
* Launch the application.
*
* @throws AWTException
*/
public static void main(String[] args) throws AWTException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dingshidianji frame = new dingshidianji();
frame.setVisible(true);
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
SubstanceLookAndFeel.setCurrentTheme(new SubstanceTerracottaTheme());
SubstanceLookAndFeel.setSkin(new EmeraldDuskSkin());
SubstanceLookAndFeel.setCurrentButtonShaper(new ClassicButtonShaper());
SubstanceLookAndFeel.setCurrentWatermark(new SubstanceBubblesWatermark());
SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
SubstanceLookAndFeel.setCurrentGradientPainter(new StandardGradientPainter());
SubstanceLookAndFeel.setCurrentTitlePainter(new FlatTitlePainter());
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteGlassLookAndFeel");
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws InterruptedException
*/
public dingshidianji() throws InterruptedException {
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) screensize.getWidth();
int y = (int) screensize.getHeight();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(x - 244, y - 260, 244, 220);
// 260包含下方菜单栏
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(96, 38, 98, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(96, 62, 98, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel = new JLabel("X\u5750\u6807:");
lblNewLabel.setBounds(52, 41, 54, 15);
contentPane.add(lblNewLabel);
JLabel lblY = new JLabel("Y\u5750\u6807:");
lblY.setBounds(52, 65, 54, 15);
contentPane.add(lblY);
textField_2 = new JTextField();
textField_2.setBounds(96, 86, 98, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel label = new JLabel("\u5EF6 \u65F6:");
label.setBounds(52, 89, 54, 15);
contentPane.add(label);
final JRadioButton radioButton = new JRadioButton("\u5355\u51FB");
radioButton.setBounds(52, 9, 54, 23);
contentPane.add(radioButton);
final JRadioButton radioButton_1 = new JRadioButton("\u53CC\u51FB");
radioButton_1.setBounds(114, 9, 59, 23);
contentPane.add(radioButton_1);
bg = new ButtonGroup();
bg.add(radioButton_1);
bg.add(radioButton);
JButton button = new JButton("\u63D0\u4EA4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (radioButton.isSelected()) {
Timer timer = new Timer();
shijian = Integer.parseInt(textField_2.getText().trim()) * 1000;
new Thread(){
public void run() {
try {
daojishi(shijian);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
timer.schedule(new TimerTask() {
@Override
public void run() {
int xzb = Integer.parseInt(textField.getText().trim());
int yzb = Integer.parseInt(textField_1.getText().trim());
Robot rob;
try {
rob = new Robot();
rob.mouseMove(xzb, yzb);
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, shijian);
} else if (radioButton_1.isSelected()) {
Timer timer = new Timer();
shijian = Integer.parseInt(textField_2.getText().trim()) * 1000;
new Thread(){
public void run() {
try {
daojishi(shijian);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
timer.schedule(new TimerTask() {
@Override
public void run() {
int xzb = Integer.parseInt(textField.getText().trim());
int yzb = Integer.parseInt(textField_1.getText().trim());
Robot rob;
try {
rob = new Robot();
rob.mouseMove(xzb, yzb);
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
rob.delay(100);
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, shijian);
}
}
});
button.setBounds(104, 117, 90, 23);
contentPane.add(button);
JLabel label_1 = new JLabel("\u5C4F\u5E55\u5750\u6807:");
label_1.setBounds(10, 150, 54, 15);
contentPane.add(label_1);
lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setBounds(64, 150, 164, 15);
contentPane.add(lblNewLabel_1);
label_2 = new JLabel("");
label_2.setBounds(10, 121, 96, 15);
contentPane.add(label_2);
new Thread() {
public void run() {
zb(true);
};
}.start();
}
private void daojishi(int time) throws InterruptedException
{
int temp=time/1000;
while (temp>0) {
label_2.setText("剩"+temp--+"秒执行操作");
Thread.sleep(1000);
if(temp==0)
{
label_2.setText("程序已经启动");
}
}
}
private static void zb(boolean flag) {
// TODO Auto-generated method stub
while (flag) {
Point mousepoint = MouseInfo.getPointerInfo().getLocation();
str = "X坐标是:" + mousepoint.x + "|Y坐标是:" + mousepoint.y;
lblNewLabel_1.setText(str);
System.out.println("X坐标是:" + mousepoint.x + "|Y坐标是:" + mousepoint.y);
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
主要用到的Robot类 模拟鼠标操作
程序加入了美化界面包 substance.jar 自行在网上下载 导入
实现定时在某区域点击执行点击事件,主要是想偷懒,半夜下载百度云资源,控制开关,所以写了这样一个demo
有需要注意:delay方法最大支持60000毫秒延时 也就是60秒 所以使用了Timer计时
自动获取鼠标在屏幕上的坐标点 倒计时时间
因为只是想实现功能 所以没有考虑代码的可读性,此demo可以导入直接使用
仅供参考
该博客介绍了一个JAVA程序,使用Robot类模拟鼠标操作,实现延时点击功能。程序设计用于在指定区域自动执行点击事件,如在夜间自动控制下载任务。注意,delay方法的最大延时为60秒,因此采用了Timer计时器。虽然代码可读性不高,但可以直接运行使用。
154

被折叠的 条评论
为什么被折叠?



