利用Robot类,进行自动点击

package robot;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import javax.swing.*;

public class RobotTest2 {
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   @Override
   public void run() {
    ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice screen = environment.getDefaultScreenDevice();
    
    try {
     Robot robot = new Robot(screen);
     runTest(robot);
    } catch (AWTException e) {
     e.printStackTrace();
    }
   }
  });
 }
 
 public static void runTest(Robot robot) {
  //空格
  robot.keyPress(' ');
  robot.keyRelease(' ');
  
  //按TAB键
  robot.delay(2000);
  robot.keyPress(KeyEvent.VK_TAB);
  robot.keyRelease(KeyEvent.VK_TAB);
  robot.keyPress(' ');
  robot.keyRelease(' ');
  
  //点击鼠标左键
  robot.delay(2000);
  robot.mouseMove(200, 50);
  robot.keyPress(InputEvent.BUTTON1_MASK);
  robot.keyRelease(InputEvent.BUTTON1_MASK);
  
  //截取屏幕
  robot.delay(2000);
  BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300));
  
  ImageFrame frame = new ImageFrame(image);
  frame.setVisible(true);
 }
}

class ImageFrame extends JFrame {
 public ImageFrame(Image image) {
  setTitle("Capture");
  setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  
  JLabel label = new JLabel(new ImageIcon(image));
  add(label);
 }
 
 public static final int DEFAULT_WIDTH = 450;
 public static final int DEFAULT_HEIGHT = 350;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值