jframe 显示html
时间: 2023-11-03 09:55:20 浏览: 58
要在 JFrame 中显示 HTML,可以使用 JEditorPane 组件。JEditorPane 支持多种格式,包括 HTML。以下是一个简单的示例代码:
```java
import javax.swing.*;
import java.awt.*;
public class HTMLViewer extends JFrame {
public HTMLViewer() {
super("HTML Viewer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
// 创建一个 JEditorPane 组件
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
// 将 JEditorPane 放在 JScrollPane 中,以便可以滚动
JScrollPane scrollPane = new JScrollPane(editorPane);
getContentPane().add(scrollPane, BorderLayout.CENTER);
// 加载并显示 HTML
try {
editorPane.setPage("https://www.baidu.com/");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HTMLViewer viewer = new HTMLViewer();
viewer.setVisible(true);
}
}
```
在上面的示例代码中,我们创建了一个 JFrame,并在其中创建了一个 JEditorPane。然后,将 JEditorPane 放在一个 JScrollPane 中,以便可以滚动。最后,使用 setPage() 方法将指定的 URL 加载到 JEditorPane 中,从而显示 HTML 内容。请注意,JEditorPane 也支持本地文件,因此您可以将本地 HTML 文件的路径传递给 setPage() 方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)