使用 Prism.js 在 WebView 中显示 HTML
Show HTML in WebView using Prism.js
我无法使用 Prism.js
在 JavaFX
的 WebVew
中显示 HTML
。我能够成功显示 Java
示例,但 HTML
示例似乎想要显示 HTML,因为它在您访问网站时出现,而不是仅仅显示语法字符串。使用 Prism.js
在 JavaFX
的 WebView
中显示 HTML
的正确方法是什么?
Java示例(正确显示)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication319 extends Application
{
@Override
public void start(Stage primaryStage)
{
String javaString = "/* HelloWorld.java\n"
+ " */\n"
+ "\n"
+ "public class HelloWorld\n"
+ "{\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello World!\");\n"
+ " }\n"
+ "}";
String htmlString = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>"
+ "<link href=\"" + getClass().getResource("prism.css") + "\"" + " rel=\"stylesheet\"" + " type=\"text/css\"" + " />\n"
+ "<script src=\"" + getClass().getResource("prism.js") + "\"" + " type=\"text/javascript\"" + "></script>\n"
+ "</head>"
+ "<body>\n"
+ "\n"
+ "<h1>My First Heading</h1>\n"
+ "\n"
+ "<p>My first paragraph.</p>\n"
+ "\n"
+ "<pre>"
+ " <code class=\"language-java\">\n"
+ javaString
+ "</code>\n"
+ "</pre>\n"
+ "</body>\n"
+ "</html>";
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.setJavaScriptEnabled(true);
engine.loadContent(htmlString);
StackPane root = new StackPane(webView);
Scene scene = new Scene(root, 500, 400);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
HTML 示例(显示不正确)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication319 extends Application
{
@Override
public void start(Stage primaryStage)
{
String htmlStringPreCode = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>\n"
+ "</head>\n"
+ "<body>\n"
+ "This is a Test\n"
+ "</body>\n"
+ "</html>\n";
String htmlString = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>"
+ "<link href=\"" + getClass().getResource("prism.css") + "\"" + " rel=\"stylesheet\"" + " type=\"text/css\"" + " />\n"
+ "<script src=\"" + getClass().getResource("prism.js") + "\"" + " type=\"text/javascript\"" + "></script>\n"
+ "</head>"
+ "<body>\n"
+ "\n"
+ "<h1>My First Heading</h1>\n"
+ "\n"
+ "<p>My first paragraph.</p>\n"
+ "\n"
+ "<pre>"
+ " <code class=\"language-html\">\n"
+ htmlStringPreCode
+ "</code>\n"
+ "</pre>\n"
+ "</body>\n"
+ "</html>";
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.setJavaScriptEnabled(true);
engine.loadContent(htmlString);
StackPane root = new StackPane(webView);
Scene scene = new Scene(root, 500, 400);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
预期输出
Java: 1.8.0_171
Prism.js图片中的下载也选择了Java
Html代码需要先转义(这样才能显示为纯文本)
看到这个question,两种解决方案都有效:
htmlStringPreCode = htmlStringPreCode.replace("<", "<");
//or
htmlStringPreCode = "<script type=\"prism-html-markup\">" + htmlStringPreCode + "</script>";
我无法使用 Prism.js
在 JavaFX
的 WebVew
中显示 HTML
。我能够成功显示 Java
示例,但 HTML
示例似乎想要显示 HTML,因为它在您访问网站时出现,而不是仅仅显示语法字符串。使用 Prism.js
在 JavaFX
的 WebView
中显示 HTML
的正确方法是什么?
Java示例(正确显示)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication319 extends Application
{
@Override
public void start(Stage primaryStage)
{
String javaString = "/* HelloWorld.java\n"
+ " */\n"
+ "\n"
+ "public class HelloWorld\n"
+ "{\n"
+ " public static void main(String[] args) {\n"
+ " System.out.println(\"Hello World!\");\n"
+ " }\n"
+ "}";
String htmlString = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>"
+ "<link href=\"" + getClass().getResource("prism.css") + "\"" + " rel=\"stylesheet\"" + " type=\"text/css\"" + " />\n"
+ "<script src=\"" + getClass().getResource("prism.js") + "\"" + " type=\"text/javascript\"" + "></script>\n"
+ "</head>"
+ "<body>\n"
+ "\n"
+ "<h1>My First Heading</h1>\n"
+ "\n"
+ "<p>My first paragraph.</p>\n"
+ "\n"
+ "<pre>"
+ " <code class=\"language-java\">\n"
+ javaString
+ "</code>\n"
+ "</pre>\n"
+ "</body>\n"
+ "</html>";
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.setJavaScriptEnabled(true);
engine.loadContent(htmlString);
StackPane root = new StackPane(webView);
Scene scene = new Scene(root, 500, 400);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
HTML 示例(显示不正确)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication319 extends Application
{
@Override
public void start(Stage primaryStage)
{
String htmlStringPreCode = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>\n"
+ "</head>\n"
+ "<body>\n"
+ "This is a Test\n"
+ "</body>\n"
+ "</html>\n";
String htmlString = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>"
+ "<link href=\"" + getClass().getResource("prism.css") + "\"" + " rel=\"stylesheet\"" + " type=\"text/css\"" + " />\n"
+ "<script src=\"" + getClass().getResource("prism.js") + "\"" + " type=\"text/javascript\"" + "></script>\n"
+ "</head>"
+ "<body>\n"
+ "\n"
+ "<h1>My First Heading</h1>\n"
+ "\n"
+ "<p>My first paragraph.</p>\n"
+ "\n"
+ "<pre>"
+ " <code class=\"language-html\">\n"
+ htmlStringPreCode
+ "</code>\n"
+ "</pre>\n"
+ "</body>\n"
+ "</html>";
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.setJavaScriptEnabled(true);
engine.loadContent(htmlString);
StackPane root = new StackPane(webView);
Scene scene = new Scene(root, 500, 400);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
预期输出
Java: 1.8.0_171
Prism.js图片中的下载也选择了Java
Html代码需要先转义(这样才能显示为纯文本)
看到这个question,两种解决方案都有效:
htmlStringPreCode = htmlStringPreCode.replace("<", "<");
//or
htmlStringPreCode = "<script type=\"prism-html-markup\">" + htmlStringPreCode + "</script>";