如何使我的webbrowser能够在Java中使用JavaScript?

如何使我的webbrowser能够在Java中使用JavaScript?,java,javascript,swing,mozilla,chromium,Java,Javascript,Swing,Mozilla,Chromium,在我的Java web浏览器中,javascript alert()不起作用。如何激活它 例如: 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; impor

在我的Java web浏览器中,javascript alert()不起作用。如何激活它

例如:

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);
      }
    });
  }
}
包www;
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入javax.swing.JEditorPane;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.SwingUtilities;
导入javax.swing.text.Document;
导入javax.swing.text.html.HTMLEditorKit;
导入javax.swing.text.html.StyleSheet;
公共类HtmlEditorKitTest{
公共静态void main(字符串[]args){
新的HtmlEditorKitTest();
}
公共HtmlEditorKitTest(){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JEditorPane JEditorPane=新的JEditorPane();
jEditorPane.setEditable(false);
JScrollPane scrollPane=新的JScrollPane(jEditorPane);
HTMLEditorKit=新的HTMLEditorKit();
jEditorPane.setEditorKit(工具包);
StyleSheet StyleSheet=kit.getStyleSheet();
addRule(“body{color:#000;字体系列:times;margin:4px;}”);
addRule(“h1{color:blue;}”);
addRule(“h2{color:#ff0000;}”);
addRule(“pre{font:10px;颜色:黑色;背景色:#fafafafa;}”);
字符串htmlString=“\n”
+“\n”
+“警报(‘显示Javascript是否工作’);\n”
+“欢迎!\n”
+“H2头\n”
+“文本描述

\n” +“

\n” +“\n”; Document doc=kit.createDefaultDocument(); jEditorPane.setDocument(doc); jEditorPane.setText(htmlString); JFrame j=新的JFrame(“Java中的WebBrowser”); j、 getContentPane().add(滚动窗格,BorderLayout.CENTER); j、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j、 设置尺寸(新尺寸(300200)); j、 setLocationRelativeTo(空); j、 setVisible(真); } }); } }
将某个东西称为web浏览器并不意味着它就是web浏览器

从API:

Swing-JEditorPane文本组件通过称为EditorKit的插件机制支持不同类型的内容。由于HTML是一种非常流行的内容格式,因此默认情况下会提供一些支持。默认支持由该类提供,该类支持HTML版本3.2(带有一些扩展),并且正在向版本4.0迁移。不支持
标记,但为
标记提供了一些支持


将某个东西称为web浏览器并不意味着它就是web浏览器

从API:

Swing-JEditorPane文本组件通过称为EditorKit的插件机制支持不同类型的内容。由于HTML是一种非常流行的内容格式,因此默认情况下会提供一些支持。默认支持由该类提供,该类支持HTML版本3.2(带有一些扩展),并且正在向版本4.0迁移。不支持
标记,但为
标记提供了一些支持


你说的“网络浏览器”是什么意思?你的JFrame?也看看这个。我认为这也是相关的。你说的“网络浏览器”是什么意思?你的JFrame?也可以看到这个。我认为这也是相关的。在创建了一个功能性小程序上下文和环境之后,我在
JEditorPane
的编辑器工具包中添加了对
applet
元素的支持(用于)。我实现了它,但这是整个过程中最困难的部分。在创建了一个功能性小程序上下文和环境之后,我开始在
JEditorPane
的编辑器工具包中添加对
applet
元素的支持(用于)。我做到了,但这是整个过程中最艰难的部分。