如何让我的网络浏览器能够在 Java 中使用 JavaScript?
在我的 Java Web 浏览器中,javascriptalert() 不起作用,如何使其激活?
例子:
package www;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class HtmlEditorKitTest {
public static void main(String[] args) {
new HtmlEditorKitTest();
}
public HtmlEditorKitTest() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(jEditorPane);
HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; }");
styleSheet.addRule("h1 {color: blue;}");
styleSheet.addRule("h2 {color: #ff0000;}");
styleSheet.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");
String htmlString = "<html>\n"
+ "<body>\n"
+ "<script>alert('Show Javascript works or not');</script>\n"
+ "<h1>Welcome!</h1>\n"
+ "<h2>H2 header</h2>\n"
+ "<p>Text description</p>\n"
+ "<p><a href=\"http://www.google.com/can/whynot?Oracle?/\">SiteOpen</a></p>\n"
+ "</body>\n";
Document doc = kit.createDefaultDocument();
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);
JFrame j = new JFrame("WebBrowser in Java");
j.getContentPane().add(scrollPane, BorderLayout.CENTER);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(new Dimension(300,200));
j.setLocationRelativeTo(null);
j.setVisible(true);
}
});
}
}
In my Java web browser, the javascript alert() is not working how to make it active?
Example:
package www;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class HtmlEditorKitTest {
public static void main(String[] args) {
new HtmlEditorKitTest();
}
public HtmlEditorKitTest() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(jEditorPane);
HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; }");
styleSheet.addRule("h1 {color: blue;}");
styleSheet.addRule("h2 {color: #ff0000;}");
styleSheet.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");
String htmlString = "<html>\n"
+ "<body>\n"
+ "<script>alert('Show Javascript works or not');</script>\n"
+ "<h1>Welcome!</h1>\n"
+ "<h2>H2 header</h2>\n"
+ "<p>Text description</p>\n"
+ "<p><a href=\"http://www.google.com/can/whynot?Oracle?/\">SiteOpen</a></p>\n"
+ "</body>\n";
Document doc = kit.createDefaultDocument();
jEditorPane.setDocument(doc);
jEditorPane.setText(htmlString);
JFrame j = new JFrame("WebBrowser in Java");
j.getContentPane().add(scrollPane, BorderLayout.CENTER);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(new Dimension(300,200));
j.setLocationRelativeTo(null);
j.setVisible(true);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将某个东西称为网络浏览器并不意味着它就是网络浏览器。
来自
HTMLEditorKit
API:Calling something a web browser doesn't make it a web browser.
From the
HTMLEditorKit
API: