如何用Java打开网页?

发布于 2024-07-17 12:21:42 字数 223 浏览 5 评论 0原文

有没有一种简单的方法可以在 GUI 的 JPanel 中打开网页?

如果没有,如何使用计算机的默认网络浏览器打开网页?

我希望能够用 20 行以下的代码完成一些事情,并且最多只需要创建一个类。 不过没有理由选择 20 个,只是希望代码很少......

我打算打开一个游戏指南。 该指南是在线的,有多个页面,但页面相互链接,所以我希望我只需要用我的代码调用一个 URL。

Is there a simple way to open a web page within a GUI's JPanel?

If not, how do you open a web page with the computer's default web browser?

I am hoping for something that I can do with under 20 lines of code, and at most would need to create one class. No reason for 20 though, just hoping for little code...

I am planning to open a guide to go with a game. The guide is online and has multiple pages, but the pages link to each other, so I am hoping I only have to call one URL with my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

我的黑色迷你裙 2024-07-24 12:21:42

使用默认 Web 浏览器打开网页很容易:

java.awt.Desktop.getDesktop().browse(theURI);

嵌入浏览器却不那么容易。 JEditorPane 具有一些 HTML 功能(如果我没记错的话,我有限的 Swing 知识),但它非常有限,不适合通用浏览器。

Opening a web page with the default web browser is easy:

java.awt.Desktop.getDesktop().browse(theURI);

Embedding a browser is not so easy. JEditorPane has some HTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.

我乃一代侩神 2024-07-24 12:21:42

我知道有两种标准方法:

  1. 标准 JEditorPane 组件
  2. 桌面getDesktop()browse(URI) 打开用户的默认浏览器(Java 6 或更高版本)

    很快,还会有第三个:

  3. JWebPane 组件,显然尚未发布的

JEditorPane 非常简陋; 它不处理CSS或JavaScript,你甚至必须自己处理超链接。 但与启动 FireFox 相比,您可以将其更无缝地嵌入到您的应用程序中。

以下是如何使用超链接的示例(假设您的文档不使用框架):

// ... initialize myEditorPane
myEditorPane.setEditable(false); // to allow it to generate HyperlinkEvents
myEditorPane.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
            myEditorPane.setToolTipText(e.getDescription());
        } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
            myEditorPane.setToolTipText(null);
        } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                myEditorPane.setPage(e.getURL());
            } catch (IOException ex) {
                // handle error
                ex.printStackTrace();
            }
        }
    }
});

There are two standard ways that I know of:

  1. The standard JEditorPane component
  2. Desktop.getDesktop().browse(URI) to open the user's default browser (Java 6 or later)

    Soon, there will also be a third:

  3. The JWebPane component, which apparently has not yet been released

JEditorPane is very bare-bones; it doesn't handle CSS or JavaScript, and you even have to handle hyperlinks yourself. But you can embed it into your application more seamlessly than launching FireFox would be.

Here's a sample of how to use hyperlinks (assuming your documents don't use frames):

// ... initialize myEditorPane
myEditorPane.setEditable(false); // to allow it to generate HyperlinkEvents
myEditorPane.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
            myEditorPane.setToolTipText(e.getDescription());
        } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
            myEditorPane.setToolTipText(null);
        } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                myEditorPane.setPage(e.getURL());
            } catch (IOException ex) {
                // handle error
                ex.printStackTrace();
            }
        }
    }
});
燕归巢 2024-07-24 12:21:42

如果您正在开发小程序,则可以使用 AppletContext.showDocument。 这只是一句:

getAppletContext().showDocument("http://example.com", "_blank");

如果您正在开发桌面应用程序,您可以尝试 Bare Bones 浏览器启动

If you're developing an applet, you can use AppletContext.showDocument. That would be a one-liner:

getAppletContext().showDocument("http://example.com", "_blank");

If you're developing a desktop application, you might try Bare Bones Browser Launch.

作业与我同在 2024-07-24 12:21:42

根本没有尝试过,但是来自 cobra HTML 解析器和查看器 href="http://lobobrowser.org/" rel="nofollow noreferrer">lobo 浏览器,纯 java 编写的浏览器,可能就是您想要的。 他们提供了设置在线 html 查看器的示例代码:

import javax.swing.*;
import org.lobobrowser.html.gui.*;
import org.lobobrowser.html.test.*;

public class BareMinimumTest {
  public static void main(String[] args) throws Exception {
    JFrame window = new JFrame();
    HtmlPanel panel = new HtmlPanel();
    window.getContentPane().add(panel);
    window.setSize(600, 400);
    window.setVisible(true);
    new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
    .navigate("http://lobobrowser.org/browser/home.jsp");
  }
}

haven't tried it at all, but the cobra HTML parser and viewer from the lobo browser, a browser writen in pure java, may be what you're after. They provide sample code to set up an online html viewer:

import javax.swing.*;
import org.lobobrowser.html.gui.*;
import org.lobobrowser.html.test.*;

public class BareMinimumTest {
  public static void main(String[] args) throws Exception {
    JFrame window = new JFrame();
    HtmlPanel panel = new HtmlPanel();
    window.getContentPane().add(panel);
    window.setSize(600, 400);
    window.setVisible(true);
    new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
    .navigate("http://lobobrowser.org/browser/home.jsp");
  }
}
作妖 2024-07-24 12:21:42

我不知道这样的内置是否存在,但我会使用 Runtime 类的 exec,以 iexplore.exe 或 firefox.exe 作为参数。

I don't know if such a built-in exists, but I would use the Runtime class's exec with iexplore.exe or firefox.exe as an argument.

べ繥欢鉨o。 2024-07-24 12:21:42

请尝试以下代码:

import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxExaception;

//below is the code
//whatvever the url is it has to have https://

Desktop d = Desktop.getDesktop();
try {
    d.browse(new URI("http://google.com"));
} catch (IOException | URISyntaxException e2) {
    e2.printStackTrace();
} 

Please try below code :

import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxExaception;

//below is the code
//whatvever the url is it has to have https://

Desktop d = Desktop.getDesktop();
try {
    d.browse(new URI("http://google.com"));
} catch (IOException | URISyntaxException e2) {
    e2.printStackTrace();
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文