简体   繁体   English

在JavaFX阶段使用java.awt.Robot安全吗?

[英]Is it safe to use java.awt.Robot inside JavaFX stage?

I have the following code inside a stage init() method, to press "SPACE" when the window opens: 我在阶段init()方法中包含以下代码,以在窗口打开时按“ SPACE”:

Platform.runLater(() -> {
    try {
        java.awt.Robot r = new java.awt.Robot();
        r.keyPress(java.awt.event.KeyEvent.VK_SPACE);
        r.keyRelease(java.awt.event.KeyEvent.VK_SPACE);
    } catch (Exception e) {
        e.printStackTrace();
    }
});

But as Robot belongs to java.awt package, and knowing that JavaFX runs in a different thread than Swing/awt, i don't know if this is safe or not. 但是由于Robot属于java.awt软件包,并且知道JavaFX在与Swing / awt不同的线程中运行,所以我不知道这是否安全。 All i know is that it does what i want, but will it work always? 我所知道的是它可以满足我的要求,但是它会一直有效吗? Should i replace this code for something else? 我是否应该将此代码替换为其他代码?

Yes it is, i've used it in a normal Thread (not using Platform.runLater()) and it worked fine even with UI Events like this: 是的,是的,我已经在普通线程中使用了它(不使用Platform.runLater()),即使在这样的UI事件中也可以正常工作:

static Robot robot=null;

private void initialize(){
        Thread hilo=new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    robot=new Robot();
                    robot.waitForIdle();
                } catch (AWTException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }});

        hilo.start();
}

and then the event method 然后是事件方法

public static void handleMultiSelectIn(){
        try{
            if(togleMulti.isSelected())
                robot.keyPress(KeyEvent.VK_CONTROL);
            System.out.println("In:"+togleMulti.isSelected());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

JavaFX 8 introduced a SwingNode class that provides reverse integration and enables to embed Swing components in JavaFX applications. JavaFX 8引入了SwingNode类,该类提供反向集成,并允许将Swing组件嵌入JavaFX应用程序中。
java.awt.Robot is not directly related to Swing, but can definitely affect swing components. java.awt.Robot与Swing没有直接关系,但是肯定会影响Swing组件。

SwingNode Class SwingNode类
To specify the content of the SwingNode object, call the setContent method, which accepts an instance of the javax.swing.JComponent class. 要指定SwingNode对象的内容,请调用setContent方法,该方法接受javax.swing.JComponent类的实例。
You can call the setContent method on either the JavaFX application thread or event dispatch thread (EDT). 您可以在JavaFX应用程序线程或事件调度线程(EDT)上调用setContent方法。
However, to access the Swing content, ensure that your code runs on EDT, because the standard Swing threading restrictions apply. 但是, 要访问 Swing内容,请确保您的代码在EDT上运行,因为存在标准的Swing线程限制。

You can check the official JavaFX docs over at: 您可以在以下位置查看JavaFX官方文档:
Embedding Swing Content in JavaFX Applications 在JavaFX应用程序中嵌入Swing内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM