📄 autoclick.java
字号:
/*This program is written for repeating works. It can click buttons instead of hunman being.
* function : 1.弹出人机界面
* 2.输入数据合法性检验
* 3.自动点击
* 4.线程并行执行
* 5.数据显示与计算
* 6.按钮动作
*
* Program name : autoclick
* Version : 2.0
* Programmer : caijie
* Date : 2007.9.1
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//autoclick主函数,用于创建主体外框
public class autoclick
{
public static void main( String [] args ) throws Exception
{
JFrame frame = new JFrame("自动点击器");
frame.add( new HelloComponent4());
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(300, 300);
frame.setVisible(true);
}
}
//定义窗口
class HelloComponent4 extends JComponent
implements MouseMotionListener, ActionListener, Runnable
{
JButton theButton;
JTextField rangeField;
JLabel num_field;
int num, i;
boolean flag, passed = false;
public HelloComponent4( ) throws Exception {
Box form = Box.createVerticalBox( );
form.add( new JLabel("请输入需要点击的次数:(1-10000)") );
// 初始数据
rangeField = new JTextField("2");
//校验数据数据合法性,如果焦点移动时数据非法,则鸣叫
rangeField.setInputVerifier( new InputVerifier( ) {
//InputVerifier函数在焦点移动的时候会调用下面的函数,用于检测数据合法性
public boolean verify( JComponent comp ) {
JTextField field = (JTextField)comp;
try {
int n = Integer.parseInt(field.getText( ));
passed = ( 0 <= n && n <= 10000 );
} catch (NumberFormatException e) { }
if ( !passed ) {
comp.getToolkit( ).beep( );
field.selectAll( );
}
return passed;
}
} );
form.add(rangeField);
//增加按钮,并实现动作时的处理
theButton = new JButton(" 开始点击 ");
setLayout( new FlowLayout( ) );
form.add( theButton );
theButton.addActionListener( this );
addMouseMotionListener( this );
//增加显示点击次数的组件,并使其初值为0
form.add( new JLabel("剩余的点击次数:") );
num_field = new JLabel();
num_field.setText("0");
form.add(num_field);
add( form );
//建立新线程
Thread t = new Thread( this );
t.start( );
System.out.println("start");
}
public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
//增加按钮动作后的处理
public void actionPerformed( ActionEvent e )
{
if ( e.getSource( ) == theButton && passed == true) {
//按钮动作后,将相应的数据写入变量并送至窗口组件显示
num = Integer.parseInt(rangeField.getText( ));
num_field.setText(String.valueOf(num));
flag = true;
}
}
//thread进程
public void run( ) {
while(true) {
//动作鼠标
System.out.println("run...");
if (flag == true){
try {
Robot r = new Robot();
while (num > 0){
Thread.sleep(2000);
r.mousePress( InputEvent.BUTTON1_MASK );
r.mouseRelease( InputEvent.BUTTON1_MASK );
num = num - 1;
num_field.setText(String.valueOf(num));
}
} catch (Exception exception){}
// System.out.println("111111");//程序打印
flag = false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -