QQ刷屏机器人
思路:收集内容,放入剪贴板,启动线程模拟按键代码如下:
程序代码:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.datatransfer.*; /** *@author Jiang *@Date 2021/7/11 15:55 *@version QQRobot1.0 */ public class QQRobot_1 extends JFrame implements Runnable { //等待时间 private int waitTime = 0; //内容 private JLabel textLabel = null; private JTextField textInput = null; //次数 private JLabel numLabel = null; private JTextField numInput = null; //高级设置 private JButton settingButton = null; //开始、暂停 private JButton startButton = null; private JButton suspendButton = null; //退出 private JButton exitButton = null; //机器人 private Robot robot = null; //线程 private Thread thread = null; //作者信息 private JButton infoButton = null; //构造方法 private QQRobot_1() { //机器人 try { this.robot = new Robot(); }catch(Exception e) { JOptionPane.showMessageDialog(this,"程序错误,请重启!"); System.exit(1); } //等待时间 this.waitTime = 1000; //窗口设置 this.setSize(250,200); this.setResizable(false); this.setLayout(new FlowLayout()); this.setTitle("QQRobot"); //内容 textLabel = new JLabel("内容:"); textInput = new JTextField(17); textInput.setText("感谢使用!"); //次数 numLabel = new JLabel("次数:"); numInput = new JTextField(17); numInput.setText("50"); //高级设置 settingButton = new JButton("高级设置"); settingButton.addActionListener((ActionEvent e)-> { setting.setVisible(true); }); //开始,暂停 startButton = new JButton("开始"); startButton.addActionListener((ActionEvent e)-> { thread = new Thread(this); thread.start(); }); suspendButton = new JButton("取消"); suspendButton.addActionListener((ActionEvent e)-> { if(thread!=null) { thread.stop(); thread = null; }else { return; } }); infoButton = new JButton("关于"); infoButton.addActionListener((ActionEvent e)-> { new Info(); }); exitButton = new JButton("退出"); exitButton.addActionListener((ActionEvent e)-> { System.exit(0); }); //组装 this.add(textLabel); this.add(textInput); this.add(numLabel); this.add(numInput); this.add(settingButton); this.add(startButton); this.add(suspendButton); this.add(infoButton); this.add(exitButton); } //高级设置 private Setting setting = new Setting(); private class Setting extends JFrame { //延迟设置 public JLabel delayLabel = null; public JTextField delayInput = null; //等待时间 public JLabel waitLabel = null; public JTextField waitInput = null; //确认、取消、应用 public JButton confirmButton = null; public JButton cancelButton = null; public JButton applyButton = null; //构造方法 public Setting() { this.setLayout(new FlowLayout()); //延迟 this.delayLabel = new JLabel("延迟"); this.delayInput = new JTextField(17); this.delayInput.setText("50"); //等待时间 this.waitLabel = new JLabel("等待"); this.waitInput = new JTextField(17); this.waitInput.setText("1000"); //确认、取消、应用 this.confirmButton = new JButton("确认"); confirmButton.addActionListener((ActionEvent e)-> { try { QQRobot_1.this.robot.setAutoDelay(Integer.valueOf(delayInput.getText()));//设置延迟 QQRobot_1.this.waitTime = Integer.valueOf(waitInput.getText()); this.setVisible(false); }catch(Exception ee) { JOptionPane.showMessageDialog(this,"设置失败"); } }); this.cancelButton = new JButton("取消"); cancelButton.addActionListener((ActionEvent e)-> { this.delayInput.setText("50");//恢复文字 this.waitInput.setText("1000"); this.setVisible(false);//关闭窗口 }); this.applyButton = new JButton("应用"); applyButton.addActionListener((ActionEvent e)-> { try { QQRobot_1.this.robot.setAutoDelay(Integer.valueOf(delayInput.getText()));//设置延迟 QQRobot_1.this.waitTime = Integer.valueOf(waitInput.getText()); }catch(Exception eee) { JOptionPane.showMessageDialog(this,"设置失败"); } }); //组装 this.setSize(250,150); this.add(delayLabel); this.add(delayInput); this.add(waitLabel); this.add(waitInput); this.add(confirmButton); this.add(cancelButton); this.add(applyButton); } } public void run() { try { Thread.sleep(this.waitTime); var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(new StringSelection(textInput.getText()),null); var number = Integer.valueOf(numInput.getText()); for(int i=0;i<number;i++) { this.robot.keyPress(KeyEvent.VK_CONTROL); this.robot.keyPress(KeyEvent.VK_V); this.robot.keyRelease(KeyEvent.VK_CONTROL); this.robot.keyRelease(KeyEvent.VK_V); this.robot.keyPress(KeyEvent.VK_ENTER); this.robot.keyRelease(KeyEvent.VK_ENTER); } }catch(Exception e) { JOptionPane.showMessageDialog(this,"程序错误"); return; } } public static void main(String[] args) { new QQRobot_1().setVisible(true); } private class Info extends JFrame { public Info() { this.setTitle("关于"); this.setLayout(new FlowLayout()); this.setSize(150,140); this.add(new JLabel("作者QQ&邮箱")); this.add(new JLabel("2794942751")); this.add(new JLabel("JiaJinz21@)); this.setDefaultCloseOperation(0); JButton ExitButton = new JButton("退出"); ExitButton.addActionListener((ActionEvent e)-> { this.setVisible(false); }); this.add(ExitButton); this.setResizable(false); this.setVisible(true); } } }