栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

《Java 核心技术 卷1》 笔记 第11章 异常、日志、断言和调试(10)Robot 模拟键盘鼠标事件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《Java 核心技术 卷1》 笔记 第11章 异常、日志、断言和调试(10)Robot 模拟键盘鼠标事件

 

 

 11.6.3 AWT 的 Robot 类

Robot 类

版本:JDK 1.3+

用途:键盘、鼠标事件发送给 AWT ,自动对用户界面检测

获取屏幕设备:

GraphicsEnvironment enviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = environment.getDefaultScreenDevice();

Robot 对象创建:

Robot robot = new Robot(screen);

模拟键盘按下、释放:

robot.keyPress(KeyEvent.VK_TAB);

robot.keyRelease(KeyEvent.VK_TAB);

模拟鼠标移动、按下、释放:

robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

操作间延迟:

Robot.delay(1000);

总体代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.beans.EventHandler;
 
public class RobotTest {
    public static void main(String[] args) throws AWTException {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Buttonframe frame = new Buttonframe();
                frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
                frame.setVisible(true);
 
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        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(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.delay(1000);
 
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
 
        robot.delay(2000);
        robot.mouseMove(490 ,60);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
        robot.delay(2000);
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                BufferedImage image = robot.createScreenCapture(new Rectangle(0,0,400,300));
                Imageframe frame = new Imageframe(image);
                frame.setVisible(true);
            }
        });
 
    }
}
 
class Imageframe extends Jframe {
    private static final int W = 450;
    private static final int H = 350;
 
    public Imageframe(Image image){
        setTitle("Capture");
        setSize(W,H);
 
        JLabel label = new JLabel(new ImageIcon(image));
        add(label);
    }
}
 
 
class Buttonframe extends Jframe implements ActionListener {
    public static final int W = 300;
    public static final int H = 200;
    private JPanel buttonPanel;
 
    public Buttonframe() {
        setTitle("ButtonTest");
 
 
        setSize(W, H);
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension d = t.getScreenSize();
//        setLocation(((int) d.getWidth() - W) / 2, ((int) d.getHeight() - H) / 2);
        JButton yellowButton = new JButton("Yellow");
        JButton blueButton = new JButton("Blue");
        JButton redButton = new JButton("Red");
 
        buttonPanel = new JPanel();
        buttonPanel.add(yellowButton);
        buttonPanel.add(blueButton);
        buttonPanel.add(redButton);
 
        add(buttonPanel);
 
        //与下面三个this等价
        //用这个方法找位置
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println(e.getX());
                System.out.println(e.getY());
            }
        });
        yellowButton.addActionListener(EventHandler.create(ActionListener.class, this, "setTitle", "source.text"));
        blueButton.addActionListener(EventHandler.create(ActionListener.class, this, "setTitle", "source.text"));
        redButton.addActionListener(EventHandler.create(ActionListener.class, this, "setTitle", "source.text"));
 
//        yellowButton.addActionListener(this);
//        blueButton.addActionListener(this);
//        redButton.addActionListener(this);
 
    }
 
    public void actionPerformed(ActionEvent event) {
 
        setTitle(((JButton) event.getSource()).getText());
    }
}

它有几个关注点:

作者给的程序本人没有跑通,开了几个EventQueue 勉强跑出来Robot 点击位置是相对位置,大概是相对于当前鼠标位置,所以只能各个系统自己调(这只能测键盘吧,测鼠标也太坑了。。。)Buttonframe 是前面好多章节的代码,大概是第 8 章(作者用旧代码此处没有表述),为了方便读者也贴在代码中了

 

 

相关内容:选择 《Java核心技术 卷1》查找相关笔记

评论点赞收藏✨关注,是送给作者最好的礼物,愿我们共同学习,一起进步

如果对作者发布的内容感兴趣,可点击下方关注公众号 钰娘娘知识汇总 查看更多作者文章哦!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号