如何让我的网络浏览器能够在 Java 中使用 JavaScript?

发布于 2024-12-23 16:04:11 字数 2206 浏览 4 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-12-30 16:04:11

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

来自 HTMLEditorKit API:

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

Calling something a web browser doesn't make it a web browser.

From the HTMLEditorKit API:

The Swing JEditorPane text component supports different kinds of content via a plug-in mechanism called an EditorKit. Because HTML is a very popular format of content, some support is provided by default. The default support is provided by this class, which supports HTML version 3.2 (with some extensions), and is migrating toward version 4.0. The <applet> tag is not supported, but some support is provided for the <object> tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文