2011-12-19 24 views
8

我喜欢旧的Java小程序。但是因为我非常喜欢JFX的工作方式,所以我想用它编写一些游戏(甚至是制作游戏的系统,谁知道?),但是我希望能够将它们发布到我的网站上。如何去做这件事?是否可以制作JavaFX Web小程序?

回答

4

是的,你可以嵌入一个JavaFX GUI到基于Swing JApplet。你可以通过使用JFXPanel来实现 - 它本质上是Swing和JavaFX面板之间的适配器。

完整的示例:
FXApplet类设置-了JavaFX的GUI:

public class FXApplet extends JApplet { 
    protected Scene scene; 
    protected Group root; 

    @Override 
    public final void init() { // This method is invoked when applet is loaded 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       initSwing(); 
      } 
     }); 
    } 

    private void initSwing() { // This method is invoked on Swing thread 
     final JFXPanel fxPanel = new JFXPanel(); 
     add(fxPanel); 

     Platform.runLater(new Runnable() { 
      @Override 
      public void run() { 
       initFX(fxPanel); 
       initApplet(); 
      } 
     }); 
    } 

    private void initFX(JFXPanel fxPanel) { // This method is invoked on JavaFX thread 
     root = new Group(); 
     scene = new Scene(root); 
     fxPanel.setScene(scene); 
    } 

    public void initApplet() { 
     // Add custom initialization code here 
    } 
} 

和测试实施它:

public class MyFXApplet extends FXApplet { 
    // protected fields scene & root are available 

    @Override 
    public void initApplet() { 
     // this method is called once applet has been loaded & JavaFX has been set-up 

     Label label = new Label("Hello World!"); 
     root.getChildren().add(label); 

     Rectangle r = new Rectangle(25,25,250,250); 
     r.setFill(Color.BLUE); 
     root.getChildren().add(r); 
    } 
} 

或者,你可以使用FXApplet gist ,其中还包括一些文件。

相关问题