java swing 展示网页
时间: 2023-09-08 16:10:12 浏览: 52
可以使用JEditorPane类来展示网页,它是Swing组件中的一个文本编辑器,可以通过设置其content type为"text/html"来将其作为一个简单的浏览器使用。以下是一个简单的例子:
```java
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
public class WebPageViewer extends JFrame {
private JEditorPane editorPane;
public WebPageViewer(String url) {
super("Web Page Viewer");
editorPane = new JEditorPane();
editorPane.setEditable(false);
try {
editorPane.setPage(new URL(url));
} catch (IOException e) {
e.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(editorPane);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new WebPageViewer("https://www.baidu.com/");
}
}
```
运行这个程序,就可以在窗口中展示百度的网页了。