- Swing 编程示例
- 示例 - 主页
- 示例 - 环境设置
- 示例 - 边框
- 示例 - 按钮
- 示例 - 复选框
- 示例 - 组合框
- 示例 - 颜料选择器
- 示例 - 对话框
- 示例 - 编辑器面板
- 示例 - 文件选择器
- 示例 - 格式文本字段
- 示例 - 框架
- 示例 - 列表
- 示例 - 布局
- 示例 - 菜单
- 示例 - 密码字段
- 示例 - 进度条
- 示例 - 滚动面板
- 示例 - 滑块
- 示例 - 旋转控件
- 示例 - 表格
- 示例 - 工具栏
- 示例 - 树
- Swing 实用资源
- Swing - 快速指南
- Swing - 实用资源
- Swing - 讨论
Swing 示例 - 通过 URL 显示 HTML 页面
以下示例展示了如何读取 URL 以在基于 swing 的应用程序中显示其页面。
我们使用以下 API。
JEditorPane − 创建编辑器框以显示 HTML 内容。
jEditorPane.setPage(String url) − 获取从 url 传递的 HTML 并显示。
示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("Swing Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame);
frame.setSize(560, 450);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(final JFrame frame){
JPanel panel = new JPanel();
LayoutManager layout = new FlowLayout();
panel.setLayout(layout);
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable(false);
try {
jEditorPane.setPage("http://www.google.com");
} catch (IOException e) {
jEditorPane.setContentType("text/html");
jEditorPane.setText("<html>Page not found.</html>");
}
JScrollPane jScrollPane = new JScrollPane(jEditorPane);
jScrollPane.setPreferredSize(new Dimension(540,400));
panel.add(jScrollPane);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
输出
swingexamples_editorpanes.htm
广告