11.6.3 AWT 的 Robot 类 Robot 类 版本:JDK 1.3+ 用途:键盘、鼠标事件发送给 AWT ,自动对用户界面检测 获取屏幕设备: GraphicsEnvironment enviroment = GraphicsEnvironment . getLocalGraphicsEnvironment (); Gra
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》查找相关笔记
评论