你如何在 Java 中打开网页?

2022-01-24 00:00:00 web java jframe

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

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?

我希望我可以用不到 20 行代码来做一些事情,并且最多需要创建一个类.20 没有理由,只是希望代码少......

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...

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

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.

推荐答案

用默认浏览器打开网页很简单:

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

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

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

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.

相关文章