14

Robot 是 AWT 库的一部分,但它似乎与该库的大多数其他部分完全不同。我正在创建一个 Swing GUI,它将 Swing 与 Java Native Access (JNA) 和 Robot 混合在一起,以允许 Java 驱动一些 MS Windows/Citrix 工作程序。我的直觉是,由于 Robot 将在“平台的本机输入队列”上排队事件,所以我要做的最后一件事就是在 EDT 上运行它,但另一方面,AWT 和 Swing 库中的大多数类应该Swing 事件线程上运行。因此,为了在我的脑海中澄清这一点,让我尽可能具体地提出一个问题:

Robot 方法(特别是按键和释放、鼠标移动、鼠标按下和释放)是否应该在 Swing 事件调度线程(EDT)上运行或关闭?

4

3 回答 3

10

Robot您提到的方法应该在 EDT 上运行。查看源代码会发现,这些“事件”方法中的每一个都有一个共同点(afterEvent调用):

public synchronized void keyPress(int keycode) {
    checkKeycodeArgument(keycode);
    peer.keyPress(keycode);
    afterEvent();
}

public synchronized void mousePress(int buttons) {
    checkButtonsArgument(buttons);
    peer.mousePress(buttons);
    afterEvent();
}

// etc

private void afterEvent() {
    autoWaitForIdle();
    autoDelay();
}

private void autoWaitForIdle() {
    if (isAutoWaitForIdle) {
        waitForIdle();
    }
}

public synchronized void waitForIdle() {
    checkNotDispatchThread();
    /* snip */
}

private void checkNotDispatchThread() {
    if (EventQueue.isDispatchThread()) {
        throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
    }
}

Robot.isAutoWaitForIdle如果您在 EDT while is上调用这些方法中的任何true一个,则会引发异常。按理说,即使isAutoWaitForIdleis false,也不应该从 EDT 调用这些方法。

于 2012-05-11T00:24:30.937 回答
6
  • API 非常准确,然后我知道如果从 EDT 调用 Robot 应该被忽略

使用类生成输入事件与将事件发布到 AWT 事件队列或 AWT 组件的不同之处在于,事件是在平台的本机输入队列中生成的。

  • 我是 Java 的新手,我的第一次接触是 Java1.6.009,然后我无法比较 AWT 和(出生时)Java1.3 中的 Swing 和 Java1.4 中的更改

我的例子

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

public class CaptureScreen implements ActionListener {

    private JFrame f = new JFrame("Screen Capture");
    private JPanel pane = new JPanel();
    private JButton capture = new JButton("Capture");
    private JDialog d = new JDialog();
    private JScrollPane scrollPane = new JScrollPane();
    private JLabel l = new JLabel();
    private Point location;

    public CaptureScreen() {
        capture.setActionCommand("CaptureScreen");
        capture.setFocusPainted(false);
        capture.addActionListener(this);
        capture.setPreferredSize(new Dimension(300, 50));
        pane.add(capture);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(pane);
        f.setLocation(100, 100);
        f.pack();
        f.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createPicContainer();
            }
        });
    }

    private void createPicContainer() {
        l.setPreferredSize(new Dimension(700, 500));
        scrollPane = new JScrollPane(l,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBackground(Color.white);
        scrollPane.getViewport().setBackground(Color.white);
        d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        d.add(scrollPane);
        d.pack();
        d.setVisible(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("CaptureScreen")) {
            Dimension d1 = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
            Robot r;
            BufferedImage bI;
            try {
                r = new Robot(); // creates robot not sure exactly how it works
                Thread.sleep(1000); // waits 1 second before capture
                bI = r.createScreenCapture(new Rectangle(d1)); // tells robot to capture the screen
                showPic(bI);
                saveImage(bI);
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
    }

    private void saveImage(BufferedImage bI) {
        try {
            ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showPic(BufferedImage bI) {
        ImageIcon pic = new ImageIcon(bI);
        l.setIcon(pic);
        l.revalidate();
        l.repaint();
        d.setVisible(false);
        location = f.getLocationOnScreen();
        int x = location.x;
        int y = location.y;
        d.setLocation(x, y + f.getHeight());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                d.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                CaptureScreen cs = new CaptureScreen();
            }
        });
    }
}
于 2012-05-06T06:52:18.993 回答
6

放大@mKorbel 的深思熟虑的答案,并确认他的经验结果,注意各种Robot方法如何委托给RobotPeer接口的内部实例,其本地实现因平台而异。此外,这些方法是同步的。合成事件都到达EventQueue,而与源无关。

于 2012-05-06T08:28:44.377 回答