如何用Java打开网页?
有没有一种简单的方法可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用默认 Web 浏览器打开网页很容易:
嵌入浏览器却不那么容易。
JEditorPane
具有一些 HTML 功能(如果我没记错的话,我有限的 Swing 知识),但它非常有限,不适合通用浏览器。Opening a web page with the default web browser is easy:
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.我知道有两种标准方法:
JEditorPane
组件桌面
。getDesktop()
。browse(URI)
打开用户的默认浏览器(Java 6 或更高版本)很快,还会有第三个:
JWebPane
组件,显然尚未发布的JEditorPane 非常简陋; 它不处理CSS或JavaScript,你甚至必须自己处理超链接。 但与启动 FireFox 相比,您可以将其更无缝地嵌入到您的应用程序中。
以下是如何使用超链接的示例(假设您的文档不使用框架):
There are two standard ways that I know of:
JEditorPane
componentDesktop
.getDesktop()
.browse(URI)
to open the user's default browser (Java 6 or later)Soon, there will also be a third:
JWebPane
component, which apparently has not yet been releasedJEditorPane
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):
如果您正在开发小程序,则可以使用 AppletContext.showDocument。 这只是一句:
如果您正在开发桌面应用程序,您可以尝试 Bare Bones 浏览器启动。
If you're developing an applet, you can use AppletContext.showDocument. That would be a one-liner:
If you're developing a desktop application, you might try Bare Bones Browser Launch.
根本没有尝试过,但是来自 cobra HTML 解析器和查看器 href="http://lobobrowser.org/" rel="nofollow noreferrer">lobo 浏览器,纯 java 编写的浏览器,可能就是您想要的。 他们提供了设置在线 html 查看器的示例代码:
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:
我不知道这样的内置是否存在,但我会使用 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.
请尝试以下代码:
Please try below code :