你如何用Java打开网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/748895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you open web pages in Java?
提问by GA Tech Mike
Is there a simple way to open a web page within a GUI's JPanel?
有没有一种简单的方法可以在 GUI 的 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...
我希望能用不到 20 行代码完成一些事情,最多需要创建一个类。没有理由 20,只是希望有一点代码......
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.
我打算打开一个游戏指南。该指南在线并有多个页面,但页面相互链接,所以我希望我只需要用我的代码调用一个 URL。
采纳答案by Joachim Sauer
Opening a web page with the default web browser is easy:
使用默认 Web 浏览器打开网页很容易:
java.awt.Desktop.getDesktop().browse(theURI);
Embedding a browser is not so easy. JEditorPane
has someHTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.
嵌入浏览器并不容易。JEditorPane
有一些HTML 能力(如果我没有记错我有限的 Swing 知识),但它非常有限,不适合通用浏览器。
回答by Michael Myers
There are two standard ways that I know of:
我知道有两种标准方法:
- The standard
JEditorPane
component Desktop
.getDesktop()
.browse(URI)
to open the user's default browser (Java 6 or later)Soon, there will also be a third:
- The
JWebPane
component, which apparently has not yet been released
- 标准
JEditorPane
组件 Desktop
.getDesktop()
.browse(URI)
打开用户的默认浏览器(Java 6 或更高版本)很快,还会有第三个:
- 该
JWebPane
组件,这显然还没有被释放
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.
JEditorPane
非常简单;它不处理 CSS 或 JavaScript,您甚至必须自己处理超链接。但与启动 FireFox 相比,您可以更无缝地将其嵌入到您的应用程序中。
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();
}
}
}
});
回答by Geo
回答by Rich Apodaca
If you're developing an applet, you can use AppletContext.showDocument. That would be a one-liner:
如果您正在开发小程序,则可以使用AppletContext.showDocument。那将是一个单行:
getAppletContext().showDocument("http://example.com", "_blank");
If you're developing a desktop application, you might try Bare Bones Browser Launch.
如果您正在开发桌面应用程序,您可以尝试Bare Bones Browser Launch。
回答by cobbal
haven't tried it at all, but the cobra HTML parser and viewerfrom 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:
根本没有尝试过,但是来自lobo 浏览器的眼镜蛇 HTML 解析器和查看器,一个用纯 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");
}
}
回答by ElementCodez
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();
}