简体   繁体   English

是否可以使用JavaFx创建Java Applet?

[英]Is it possible to create java applets using JavaFx?

I want to create an applet using Javafx Scene Builder. 我想使用Javafx Scene Builder创建一个applet。 I know the traditional way of creating an applet but then I recently started working with JavaFx and I am in love with the way you can create awesome User Interface designs using Scene Builder and then write neat customized code. 我知道创建小应用程序的传统方法,但是最近我开始使用JavaFx,我爱上了可以使用Scene Builder创建出色的用户界面设计然后编写简洁的自定义代码的方法。 So, far I can create desktop programs using JavaFx but now I want to write small utility programs and embed it on a browser. 因此,到目前为止,我可以使用JavaFx创建桌面程序,但是现在我想编写小型实用程序并将其嵌入浏览器中。 I honestly dislike the idea of writing codes to create the UI of an applet, so I was thinking if there was a way to create an applet using Javafx. 老实说,我不喜欢编写代码来创建小程序的UI的想法,因此我在考虑是否存在使用Javafx创建小程序的方法。

PS - I am new to Stackoverflow. PS-我是Stackoverflow的新手。 So, please forgive any mistakes I make. 因此,请原谅我犯的任何错误。

Applets are a dead technology, so you should forget them altogether. 小程序是一项过时的技术,因此您应该完全忘记它们。 Support for applets has been removed at least in the newest Chrome browser, and most likely others too. 至少在最新的Chrome浏览器中以及最有可能的其他浏览器中,都已删除了对applet的支持。

It's not possible to create applets via JavaFX either. 也不能通过JavaFX创建小程序。

It can be done thanks to a JFXPanel which allows to embed a JavaFX component into a Swing component and from a Swing component you can easily create an Applet . JFXPanel可以完成此操作,该JFXPanel允许将JavaFX组件嵌入到Swing组件中,并且可以从Swing组件中轻松创建Applet

Here is an example that draws a Java FX Rectangle into an Applet : 这是一个将Java FX Rectangle绘制到Applet的示例:

public class Main extends Applet {

    private void initAndShowGUI() {
        // This method is invoked on Swing thread
        final JFXPanel fxPanel = new JFXPanel();
        fxPanel.setPreferredSize(new Dimension(100, 100));
        add(fxPanel);
        setVisible(true);
        Platform.runLater(
            new Runnable() {
                @Override
                public void run() {
                    initFX(fxPanel);
                }
            }
        );
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on JavaFX thread
        final Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene() {
        final Rectangle rectangle = new Rectangle(100.0, 100.0);
        rectangle.setFill(Color.BLACK);
        return new Scene(new VBox(rectangle));
    }

    @Override
    public void init() {
        SwingUtilities.invokeLater(
            new Runnable() {
                @Override
                public void run() {
                    initAndShowGUI();
                }
            }
        );
    }
}

However Applets are outdated and will be removed soon so if you can avoid using them, just don't use them. 但是, Applet已过时 ,将很快被删除,因此,如果您可以避免使用它们,请不要使用它们。 Alternatively you can use the Deployment Toolkit library to embed your JavaFX application in a web page or launch it from a browser, more details about this approach here . 或者,您可以使用Deployment Toolkit libraryJavaFX application嵌入到网页中,或从浏览器中启动它,有关此方法的更多详细信息,请参见此处

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

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