在Swing和Swt中使用JavaFX

来源:互联网 发布:10大网络恐怖推理图 编辑:程序博客网 时间:2024/06/01 09:32

本人从08年12月份,JavaFX发布第一个版本开始关注它的发展。


 算算到现在,也差不多是第五个年头了。期间经历了一些动荡,但JavaFX还是坚持着发展了下来,也经历了很多改变(这也是Java技术的特点,不会像微软对技术的抛弃和更新换代很频繁),由以前顺应富互联网技术发展的单纯的JavaFX脚本语言,到现在已经变为完全用于取代Swing的技术。

  

 虽然是为了取代Swing,但任何的技术为了不造成很大的影响,都必须有一个逐步换代的过程。如果单纯的从JDK中剔除Swing而增加JavaFX的话,将会造成世界数不清的程序无法运行,影响不可估量(就像JDK中有很多以前不合理的旧的类库,就算有新的可替代的方案,也不能将以前的完全删除,所以JDK其实越来越臃肿)。这里,我们将会介绍一下如何在Swing中使用JavaFX。


  首先我们要明白,不管是Swing还是JavaFX,GUI的构建必须要各自的EDT(事件分发线程)中进行,这也是在其他线程中操作UI会出现error的原因。


  而在JavaFX中,新增加了JFXPanel。这个组件是JavaFX和Swing进行混合编程的桥梁。

  下面我们来看看JFXPanel的继承关系。

  

  


  大家可以看到,JFXPanel是继承于JComponent的。而JavaFX的UI控件都是继承于javafx.scene.control.Control。所以很明显,它其实是一个Swing组件,而非JavaFX的组件。


  下面我们来看看JPanel的继承关系。

  


  继承关系基本相同,也就是说,凡是在使用JPanel的地方,我们都可以用JFXPanel来替代。JFXPanel中有一个setScene方法,可以设置其中显示的JavaFX内容。


  根据文档介绍,setScene方法可以在Swing和JavaFX的EDT中使用。也可以说,setScene才是将JavaFX和Swing结合起来的关键。


  不过由于JavaFX的GUI的构建也需要在JavaFX的事件分发线程中进行。所以我们还需要用到另外一个类Platform。

  Platform.runLater(new Runnable())中间执行的是JavaFX的事件分发线程。我们可以在Runnable中进行JavaFX的UI的创建。


  下面我们来看一个简单的示例-----在Swing中嵌入一个JavaFX的网页浏览器。

import java.awt.BorderLayout;  import java.awt.Toolkit;    import javafx.application.Platform;  import javafx.embed.swing.JFXPanel;  import javafx.event.ActionEvent;  import javafx.event.EventHandler;  import javafx.scene.Group;  import javafx.scene.Scene;  import javafx.scene.control.Button;  import javafx.scene.control.TextField;  import javafx.scene.layout.HBox;  import javafx.scene.layout.VBox;  import javafx.scene.web.WebEngine;  import javafx.scene.web.WebView;    import javax.swing.JFrame;    public class MainClass {      private static final int WIDTH = 800;      private static final int HEIGHT = 600;      private static final String url = "http://blog.csdn.net/ml3947";      private static final String urlStart = "http://";        /**      * @param args      */      public static void main(String[] args) {          JFrame frame = new JFrame("JavaFX in Swing");          final JFXPanel webBrowser = new JFXPanel();          frame.setLayout(new BorderLayout());          frame.add(webBrowser, BorderLayout.CENTER);          Platform.runLater(new Runnable() {                @Override              public void run() {                  Group root = new Group();                  Scene scene = new Scene(root, WIDTH, HEIGHT);                  webBrowser.setScene(scene);                  Double widthDouble = new Integer(WIDTH).doubleValue();                  Double heightDouble = new Integer(HEIGHT).doubleValue();                    VBox box = new VBox(10);                  HBox urlBox = new HBox(10);                  final TextField urlTextField = new TextField();                  urlTextField.setText(url);                  Button go = new Button("go");                  urlTextField.setPrefWidth(WIDTH - 70);                  urlBox.getChildren().addAll(urlTextField, go);                    WebView view = new WebView();                  view.setMinSize(widthDouble, heightDouble);                  view.setPrefSize(widthDouble, heightDouble);                  final WebEngine eng = view.getEngine();                  eng.load(url);                  root.getChildren().add(view);                    box.getChildren().add(urlBox);                  box.getChildren().add(view);                  root.getChildren().add(box);                    go.setOnAction(new EventHandler<ActionEvent>() {                      @Override                      public void handle(ActionEvent event) {                          if (!urlTextField.getText().startsWith(urlStart)) {                              eng.load(urlStart + urlTextField.getText());                          } else {                              eng.load(urlTextField.getText());                          }                      }                  });              }          });            int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;          int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          frame.setSize(WIDTH, HEIGHT);          frame.setLocation((screenWidth - WIDTH) / 2, (screenHeight - HEIGHT) / 2);          frame.setVisible(true);      }    }  

这是一个很简单的例子。

  我们创建了JFrame和JFXPanel,然后在Platform.runLater中进行Scene的创建,并设置到JFXPanel中。

  我们创建了一个Textfield用来输入网址,创建了一个JavaFX Button来跳转到网址。判断网址是不是http://开头并做简单的处理。


  下面看看运行效果:

  

  

  如图所示。



   另外,在Swt中使用的话是JFXCanvas,由于实现机制的问题,我们并不需要像在Swing中使用Platform类。而JFXCanvas也是org.eclipse.swt.widgets.Canvas的直接子类,我们可以直接使用。

  示例如下:

    

public class JFXInSwt {      private static Scene createScene() {          Group group = new Group();          Scene scene = new Scene(group);          Button button = new Button("JFX Button");          group.getChildren().add(button);          return scene;      }        public static void main(String[] args) {          Display display = new Display();          Shell shell = new Shell(display);          shell.setLayout(new FillLayout());          FXCanvas canvas = new FXCanvas(shell, SWT.NONE);          Scene scene = createScene();          canvas.setScene(scene);          shell.open();          while (!shell.isDisposed()) {              if (!display.readAndDispatch()) display.sleep();          }          display.dispose();      }  }

原文链接:http://blog.csdn.net/wingfourever/article/details/8802591#