JavaFX:使用JavaFX嵌入不同于Webview的浏览器

前端之家收集整理的这篇文章主要介绍了JavaFX:使用JavaFX嵌入不同于Webview的浏览器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 JavaFX应用程序,其中包含由内部webkit浏览器呈现的几个html,css,JS文件.现在,问题是CSS动画在JavaFX提供的webkit浏览器中没有得到顺利的呈现,但Firefox或chrome中的相同代码却相当平滑.

此外,没有持久性可用(目前在Java中使用变量,并通过JS进行持久性通信).

我正在寻找的是有任何方法来整合一些无头浏览器,或一些设置,使CSS动画更流畅.只有我遇到的是JxBrowser,但是对于个人使用来说太贵了.

代码

  1. public class Main extends Application {
  2.  
  3. private Scene scene;
  4. MyBrowser myBrowser;
  5.  
  6. String completeText = "";
  7.  
  8. @Override
  9. public void start(Stage primaryStage) throws Exception{
  10. primaryStage.setTitle("Frontend");
  11. java.net.CookieManager manager = new java.net.CookieManager();
  12. java.net.CookieHandler.setDefault(manager);
  13.  
  14. myBrowser = new MyBrowser();
  15. scene = new Scene(myBrowser,1080,1920);
  16.  
  17. primaryStage.setScene(scene);
  18. primaryStage.setFullScreen(true);
  19. primaryStage.show();
  20.  
  21. // @ being the escape character
  22. scene.setOnKeyTyped(new EventHandler<KeyEvent>() {
  23. @Override
  24. public void handle(KeyEvent event) {
  25. String text = event.getCharacter();
  26. if (text.equals("0")) {
  27. String tempText = completeText;
  28. completeText = "";
  29. processText(tempText);
  30. }else {
  31. completeText = completeText+text;
  32. }
  33. }
  34. });
  35. }
  36. }

MyBrowser:

  1. public class MyBrowser extends Region {
  2.  
  3. public MyBrowser() {
  4. webEngine.getLoadWorker().stateProperty().addListener((observable,oldValue,newValue) -> {
  5. if (newValue == Worker.State.SUCCEEDED) {
  6. JSObject window = (JSObject) webEngine.executeScript("window");
  7. window.setMember("app",this);
  8. }
  9. });
  10.  
  11.  
  12.  
  13.  
  14. URL urlHello = getClass().getResource(hellohtml);
  15.  
  16. webEngine.load(urlHello.toExternalForm());
  17. webView.setPrefSize(1080,1920);
  18. webView.setContextMenuEnabled(false);
  19. getChildren().add(webView);
  20. }

包含动画的CSS代码

  1. #ball-container.go #ball{
  2. -webkit-animation: rotating-inverse 2s ease-out 0s 1 normal;
  3. animation: rotating-inverse 2s ease-out 0s 1 normal;
  4. }
  5.  
  6.  
  7. #ball-container {
  8. height: 102px;
  9. width: 102px;
  10. position: absolute;
  11. top: -95px;
  12. left: 480px;
  13. -webkit-transition: all 0.9s ease-in-out 0s;
  14. transition: all 0.9s ease-in-out 0s;
  15. }
  16.  
  17.  
  18.  
  19. #ball-container.shake .ball-wrapper{
  20. -webkit-animation: yAxis 0.9s ease-in-out;
  21. animation: yAxis 0.9s ease-in-out;
  22. }

谢谢.

解决方法

尝试使用 Java 8u112,基于您的代码,我创建了一个工作示例(使用Java JDK 8u112 64位测试):
  1. import javafx.application.Application;
  2. import javafx.scene.Parent;
  3. import javafx.scene.Scene;
  4. import javafx.scene.web.WebEngine;
  5. import javafx.scene.web.WebView;
  6. import javafx.stage.Stage;
  7.  
  8. public class Example extends Application
  9. {
  10. public static void main(String[] args)
  11. {
  12. launch(args);
  13. }
  14.  
  15. class MyBrowser extends Parent
  16. {
  17. private WebEngine webEngine;
  18. private WebView webView;
  19.  
  20. public MyBrowser()
  21. {
  22. webView = new WebView();
  23. webEngine = webView.getEngine();
  24.  
  25. // Ugly (but easy to share) HTML content
  26. String pageContents =
  27. "<html>"
  28. + "<head>"
  29. + "<style>"
  30. + "@-webkit-keyframes mymove {"
  31. + "from {top: 0px;}"
  32. + "to {top: 50px;}"
  33. + "}"
  34. + ".Box { "
  35. + "width: 150px; "
  36. + "position: relative; "
  37. + "height: 150px; "
  38. + "background: red; "
  39. + "margin-top: 35px; "
  40. + "margin-left: auto; "
  41. + "margin-right: auto; "
  42. + "-webkit-transition: background-color 2s ease-out; "
  43. + "-webkit-transition: all 1s ease-in-out; "
  44. + "}"
  45. +".Box:hover {"
  46. +" background-color: green;"
  47. + "width:350px;"
  48. + "-webkit-animation: mymove 1s infinite;"
  49. +"}"
  50. + "</style>"
  51. + "</head>"
  52. + "<body>"
  53. + "<div class='Box'></div>"
  54. + "</body>"
  55. + "</html>";
  56.  
  57. webEngine.loadContent(pageContents);
  58. webView.setContextMenuEnabled(false);
  59. getChildren().add(webView);
  60. }
  61. }
  62.  
  63. private Scene scene;
  64. MyBrowser myBrowser;
  65.  
  66. @Override
  67. public void start(Stage primaryStage) throws Exception
  68. {
  69. primaryStage.setTitle("Frontend");
  70. myBrowser = new MyBrowser();
  71. scene = new Scene(myBrowser);
  72. primaryStage.setScene(scene);
  73. primaryStage.show();
  74. }
  75. }

我怀疑这是因为他们现在正在使用一个较新的webkit JDK-8156698,但它可能是以前的错误(可以看看8u112 bug fixes列表.

猜你在找的Java相关文章