JFXPanel
和FXCanvas
允许在其实例上设置JavaFX Scene。 下一个特定于JavaFX的代码示例中显示的方法提供了要在本示例中使用的Scene
实例(基于我的Simple JavaFX 2.0 Text Example帖子)。
提供用于集成的JavaFX场景的方法
package dustin.examples;import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;/*** Simple class intended to be used by two examples of integrating JavaFX with* Swing and with SWT. Provides single method {@code createScene()} to be used* by the classes that are examples of integrating Swing with JavaFX and SWT* with JavaFX.* * @author Dustin*/
public class TextIntegrationSceneCreator
{/*** Provides an instance of Scene with JavaFX text examples.* * @return Instance of Scene with text examples.*/public static Scene createTextScene(){final Group rootGroup = new Group();final Scene scene = new Scene(rootGroup, 800, 400, Color.BEIGE);final Text text1 = new Text(25, 25, "(2007) JavaFX based on F3");text1.setFill(Color.CHOCOLATE);text1.setFont(Font.font(java.awt.Font.SERIF, 25));rootGroup.getChildren().add(text1);final Text text2 = new Text(25, 50, "(2010) JavaFX Script Deprecated");text2.setFill(Color.DARKBLUE);text2.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));rootGroup.getChildren().add(text2);final Text text3 = new Text(25, 75, "(2011) JavaFX to be Open Sourced!");text3.setFill(Color.TEAL);text3.setFont(Font.font(java.awt.Font.MONOSPACED, 35));rootGroup.getChildren().add(text3);final Text text4 = new Text(25, 125, "(2011) JavaFX to be Standardized");text4.setFill(Color.CRIMSON);text4.setFont(Font.font(java.awt.Font.DIALOG, 40));final Effect glow = new Glow(1.0);text4.setEffect(glow);rootGroup.getChildren().add(text4);final Text text5 = new Text(25, 175, "(Now) Time for JavaFX 2.0!");text5.setFill(Color.DARKVIOLET);text5.setFont(Font.font(java.awt.Font.SERIF, FontWeight.EXTRA_BOLD, 45));final Light.Distant light = new Light.Distant();light.setAzimuth(-135.0);final Lighting lighting = new Lighting();lighting.setLight(light);lighting.setSurfaceScale(9.0);text5.setEffect(lighting);rootGroup.getChildren().add(text5);final Text text6 = new Text(25, 225, "JavaFX News at JavaOne!");text6.setFill(Color.DARKGREEN);text6.setBlendMode(BlendMode.COLOR_BURN);text6.setFont(Font.font(java.awt.Font.DIALOG_INPUT, FontWeight.THIN, 45));final Reflection reflection = new Reflection();reflection.setFraction(1.0);text6.setEffect(reflection);rootGroup.getChildren().add(text6);return scene; }
}
可以通过JavaFX类JFXPanel及其setScene(Scene)方法将JavaFX Scene集成到Swing代码中。 下一个代码清单中对此进行了演示,该清单从上一个代码清单中的方法获取特定的Scene
实例。
JavaFX / Swing与JFXPanel的集成
package dustin.examples;import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;/*** Simple class demonstrating interoperability between Swing and JavaFX. This* class is adapted from the example provided in the Javadoc documentation for* {@code javafx.embed.swing.JFXPanel}.*/
public class SwingJavaFxInteroperabilityDemo
{private static void initAndShowGUI(){// This method is invoked on Swing threadfinal JFrame frame = new JFrame("JavaFX / Swing Integrated");final JFXPanel fxPanel = new JFXPanel();frame.add(fxPanel);frame.setVisible(true);Platform.runLater(new Runnable(){@Overridepublic void run(){initFX(fxPanel);}});}private static void initFX(JFXPanel fxPanel){// This method is invoked on JavaFX threadfinal Scene scene = TextIntegrationSceneCreator.createTextScene();fxPanel.setScene(scene);}public static void main(String[] arguments){SwingUtilities.invokeLater(new Runnable(){@Overridepublic void run(){initAndShowGUI();}});}
}
接下来显示运行带有嵌入式JavaFX Scene
简单Java Swing应用程序的输出。
可以说,将SWT与JavaFX集成起来更加容易,并且在下一个代码清单中将进行演示。 与Swing集成示例一样,主要方法是调用FXCanvas的setScene(Scene)方法。
JavaFX / SWT与FXCanvas的集成
package dustin.examples;import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;/*** Simple class demonstrating interoperability between SWT and JavaFX. This* class is based on the example provided in the Javadoc documentation for* {@code javafx.embed.swt.FXCanvas}.* * @author Dustin*/
public class SwtJavaFxInteroperabilityDemo
{public static void main(String[] arguments){final Display display = new Display();final Shell shell = new Shell(display);shell.setText("JavaFX / SWT Integration");shell.setLayout(new FillLayout());final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);final Scene scene = TextIntegrationSceneCreator.createTextScene();canvas.setScene(scene);shell.open();while (!shell.isDisposed()){if (!display.readAndDispatch()) display.sleep();}display.dispose();}
}
下一个屏幕快照显示了带有嵌入式JavaFX的简单SWT应用程序的外观。
上面显示的Swing与JavaFX集成以及SWT与JavaFX集成的代码清单仅略微修改自Javadoc类JFXPanel
(Swing)和FXCanvas
(SWT)的Javadoc文档。 这些类在其文档中提供了这些示例,这很高兴,而且集成变得如此容易也非常好。 有关JavaFX / Swing集成的更详尽介绍,请参阅将JavaFX集成到Swing应用程序中 。
参考:《 实际事件启发》博客中的JCG合作伙伴 Dustin Marx的JavaFX 2.0与Swing和SWT的集成
相关文章 :
- 从JavaFX 1.3迁移到JavaFX 2.0
- JavaFX 2.0 beta示例应用程序和思考
- JavaOne正在重建动力
- 有时在Java中,一个布局管理器是不够的
翻译自: https://www.javacodegeeks.com/2012/01/integrating-javafx-20-with-swing-and.html