JEditorPaneHTMLEditorKitTable表格

wen java案例 1

本文目录导读:

JEditorPaneHTMLEditorKitTable表格

  1. 基础表格显示
  2. 带样式的高级表格
  3. 动态创建表格
  4. 可编辑表格
  5. 表格导出功能
  6. 关键特性说明:
  7. 注意事项:

我来详细解释如何使用 JEditorPane、HTMLEditorKit 来处理 HTML 表格。

基础表格显示

import javax.swing.*;
import javax.swing.text.html.*;
import java.awt.*;
public class HTMLTableExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("HTML Table Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
        editorPane.setContentType("text/html");
        // 基础表格 HTML
        String htmlContent = "<html><body>" +
            "<table border='1' style='width:100%; text-align:center;'>" +
            "<tr>" +
            "<th>姓名</th>" +
            "<th>年龄</th>" +
            "<th>城市</th>" +
            "</tr>" +
            "<tr>" +
            "<td>张三</td>" +
            "<td>25</td>" +
            "<td>北京</td>" +
            "</tr>" +
            "<tr>" +
            "<td>李四</td>" +
            "<td>30</td>" +
            "<td>上海</td>" +
            "</tr>" +
            "</table></body></html>";
        editorPane.setText(htmlContent);
        JScrollPane scrollPane = new JScrollPane(editorPane);
        frame.add(scrollPane);
        frame.setVisible(true);
    }
}

带样式的高级表格

import javax.swing.*;
import javax.swing.text.html.*;
import java.awt.*;
public class StyledHTMLTableExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Styled HTML Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 500);
        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        // 带样式和高级功能的表格
        String htmlContent = 
            "<html><head><style>" +
            "body { font-family: Arial, sans-serif; margin: 20px; }" +
            "table { border-collapse: collapse; width: 100%; }" +
            "th { background-color: #4CAF50; color: white; padding: 12px; " +
            "     border: 1px solid #ddd; }" +
            "td { padding: 10px; border: 1px solid #ddd; text-align: left; }" +
            "tr:nth-child(even) { background-color: #f2f2f2; }" +
            "tr:hover { background-color: #ddd; }" +
            "caption { font-size: 24px; margin-bottom: 15px; font-weight: bold; color: #333; }" +
            "</style></head><body>" +
            "<table>" +
            "<caption>员工信息表</caption>" +
            "<thead>" +
            "<tr>" +
            "<th>ID</th>" +
            "<th>姓名</th>" +
            "<th>职位</th>" +
            "<th>薪资</th>" +
            "<th>入职日期</th>" +
            "</tr>" +
            "</thead>" +
            "<tbody>" +
            "<tr><td>001</td><td>张三</td><td>开发工程师</td><td>¥15,000</td><td>2023-01-15</td></tr>" +
            "<tr><td>002</td><td>李四</td><td>测试工程师</td><td>¥12,000</td><td>2023-03-20</td></tr>" +
            "<tr><td>003</td><td>王五</td><td>产品经理</td><td>¥20,000</td><td>2023-02-10</td></tr>" +
            "</tbody>" +
            "<tfoot>" +
            "<tr><td colspan='5' style='text-align:center; background-color:#f0f0f0;'>" +
            "总计: 3 名员工</td></tr>" +
            "</tfoot>" +
            "</table></body></html>";
        editorPane.setText(htmlContent);
        editorPane.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(editorPane);
        frame.add(scrollPane);
        frame.setVisible(true);
    }
}

动态创建表格

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class DynamicTableExample extends JFrame {
    private JEditorPane editorPane;
    public DynamicTableExample() {
        setTitle("动态表格生成");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700, 400);
        setLayout(new BorderLayout());
        // 控制面板
        JPanel controlPanel = new JPanel();
        JButton generateBtn = new JButton("生成表格");
        JSpinner rowSpinner = new JSpinner(new SpinnerNumberModel(3, 1, 10, 1));
        JSpinner colSpinner = new JSpinner(new SpinnerNumberModel(4, 1, 8, 1));
        controlPanel.add(new JLabel("行数:"));
        controlPanel.add(rowSpinner);
        controlPanel.add(new JLabel("列数:"));
        controlPanel.add(colSpinner);
        controlPanel.add(generateBtn);
        // 编辑器
        editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        editorPane.setEditable(false);
        generateBtn.addActionListener(e -> {
            int rows = (Integer) rowSpinner.getValue();
            int cols = (Integer) colSpinner.getValue();
            generateTable(rows, cols);
        });
        add(controlPanel, BorderLayout.NORTH);
        add(new JScrollPane(editorPane), BorderLayout.CENTER);
        setVisible(true);
    }
    private void generateTable(int rows, int cols) {
        StringBuilder html = new StringBuilder();
        html.append("<html><head><style>");
        html.append("table { border-collapse: collapse; width: 100%; }");
        html.append("th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }");
        html.append("th { background-color: #2196F3; color: white; }");
        html.append("</style></head><body><table><tr>");
        // 生成表头
        for (int i = 0; i < cols; i++) {
            html.append("<th>列 ").append(i + 1).append("</th>");
        }
        html.append("</tr>");
        // 生成数据行
        for (int i = 0; i < rows; i++) {
            html.append("<tr>");
            for (int j = 0; j < cols; j++) {
                html.append("<td>R").append(i + 1).append("C").append(j + 1).append("</td>");
            }
            html.append("</tr>");
        }
        html.append("</table></body></html>");
        editorPane.setText(html.toString());
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(DynamicTableExample::new);
    }
}

可编辑表格

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;
public class EditableTableExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("可编辑表格");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        editorPane.setEditable(true); // 允许编辑
        // 使用 HTMLEditorKit
        HTMLEditorKit kit = new HTMLEditorKit();
        editorPane.setEditorKit(kit);
        String htmlContent = 
            "<html><body>" +
            "<table border='1' style='width:100%;'>" +
            "<tr><th>名称</th><th>值</th></tr>" +
            "<tr><td>用户名</td><td><input type='text' value='请输入用户名'/></td></tr>" +
            "<tr><td>密码</td><td><input type='password'/></td></tr>" +
            "<tr><td>性别</td><td>" +
            "<input type='radio' name='gender' value='男'/>男" +
            "<input type='radio' name='gender' value='女'/>女" +
            "</td></tr>" +
            "<tr><td>爱好</td><td>" +
            "<input type='checkbox'/>读书 " +
            "<input type='checkbox'/>运动 " +
            "<input type='checkbox'/>音乐" +
            "</td></tr>" +
            "<tr><td>城市</td><td>" +
            "<select>" +
            "<option>北京</option>" +
            "<option>上海</option>" +
            "<option>广州</option>" +
            "</select>" +
            "</td></tr>" +
            "</table></body></html>";
        editorPane.setText(htmlContent);
        JScrollPane scrollPane = new JScrollPane(editorPane);
        frame.add(scrollPane);
        frame.setVisible(true);
    }
}

表格导出功能

import javax.swing.*;
import java.awt.*;
import java.io.*;
public class TableExportExample extends JFrame {
    private JEditorPane editorPane;
    public TableExportExample() {
        setTitle("表格导出示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setLayout(new BorderLayout());
        editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        editorPane.setEditable(false);
        String htmlContent = 
            "<html><body>" +
            "<table border='1' width='80%' align='center'>" +
            "<tr><th>产品</th><th>价格</th><th>库存</th></tr>" +
            "<tr><td>笔记本电脑</td><td>¥5,000</td><td>100</td></tr>" +
            "<tr><td>手机</td><td>¥3,000</td><td>200</td></tr>" +
            "<tr><td>平板电脑</td><td>¥2,500</td><td>150</td></tr>" +
            "</table></body></html>";
        editorPane.setText(htmlContent);
        // 导出按钮
        JButton exportBtn = new JButton("导出为HTML");
        exportBtn.addActionListener(e -> exportTable());
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(exportBtn);
        add(new JScrollPane(editorPane), BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
        setVisible(true);
    }
    private void exportTable() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setSelectedFile(new File("table.html"));
        if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            try (PrintWriter writer = new PrintWriter(fileChooser.getSelectedFile())) {
                writer.write(editorPane.getText());
                JOptionPane.showMessageDialog(this, "表格导出成功!");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "导出失败: " + ex.getMessage());
            }
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(TableExportExample::new);
    }
}

关键特性说明:

  1. 基础表格:使用标准 HTML <table>
  2. 样式支持:支持 CSS 内联样式
  3. 动态生成:可以通过程序动态生成表格
  4. 可编辑性:支持表单元素(输入框、选择器等)
  5. 导出功能:可以将表格内容导出为 HTML 文件

注意事项:

  • JEditorPane 对 HTML 的支持有限,复杂 CSS 可能不支持
  • 建议使用内联样式而非外部 CSS
  • 对于复杂表格,考虑使用 JTable 替代
  • HTMLEditorKit 提供了基本的 HTML 渲染功能

这些示例展示了在不同场景下使用 JEditorPane 处理 HTML 表格的方法。

抱歉,评论功能暂时关闭!