本文目录导读:

在 JEditorPane 中使用 HTMLEditorKit 时,实现文本居中的操作有以下几种方式:
使用HTML标签居中(推荐)
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
public class CenteredTextExample {
public static void main(String[] args) {
JFrame frame = new JFrame("居中示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setContentType("text/html");
// 使用HTML标签实现居中
String html = "<html><body><center><h1>居中标题</h1><p>居中段落文本</p></center></body></html>";
editorPane.setText(html);
frame.add(new JScrollPane(editorPane));
frame.setVisible(true);
}
}
使用CSS样式居中
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
public class CSSCenteredExample {
public static void main(String[] args) {
JFrame frame = new JFrame("CSS居中示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setContentType("text/html");
// 使用CSS text-align: center 实现居中
String html = "<html><body style='text-align: center;'>" +
"<h1>居中标题</h1>" +
"<p>居中段落文本</p>" +
"</body></html>";
editorPane.setText(html);
frame.add(new JScrollPane(editorPane));
frame.setVisible(true);
}
}
动态添加居中文本
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class DynamicCenteredText {
public static void main(String[] args) {
JFrame frame = new JFrame("动态居中示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setContentType("text/html");
// 初始HTML
editorPane.setText("<html><body></body></html>");
// 动态添加居中文本
HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
try {
// 创建一个居中对齐的段落
doc.insertAfterEnd(doc.getCharacterElement(0),
"<p style='text-align:center;'>这是动态添加的居中文本</p>");
} catch (Exception e) {
e.printStackTrace();
}
frame.add(new JScrollPane(editorPane));
frame.setVisible(true);
}
}
使用SimpleAttributeSet实现对齐
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.HTMLEditorKit;
public class AttributeSetCentered {
public static void main(String[] args) {
JFrame frame = new JFrame("属性居中示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setContentType("text/html");
// 使用HTML框架
editorPane.setText("<html><body><p id='centerPara'>居中文本</p></body></html>");
// 或者直接使用CSS
String html = "<html><head><style>" +
"body { text-align: center; }" +
"h1 { text-align: center; color: blue; }" +
".center { text-align: center; font-size: 16px; }" +
"</style></head>" +
"<body>" +
"<h1>居中标题</h1>" +
"<p class='center'>这是居中段落</p>" +
"<p>这是普通段落(左对齐)</p>" +
"</body></html>";
editorPane.setText(html);
frame.add(new JScrollPane(editorPane));
frame.setVisible(true);
}
}
修改现有文本的对齐方式
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.HTMLEditorKit;
public class ModifyAlignment {
public static void main(String[] args) {
JFrame frame = new JFrame("修改对齐示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setContentType("text/html");
// 初始文本
editorPane.setText("<html><body><p>这是普通文本</p><p>这也是普通文本</p></body></html>");
// 通过HTML重新设置
String centeredHtml = "<html><body>" +
"<p style='text-align:center;'>现在居中的文本</p>" +
"</body></html>";
editorPane.setText(centeredHtml);
frame.add(new JScrollPane(editorPane));
frame.setVisible(true);
}
}
重要提示:
HTMLEditorKit支持的是 HTML 3.2/4.0 的子集,不是所有CSS属性都支持- 最可靠的方式是使用
<center>标签或text-align: centerCSS属性 - 动态修改内容时,建议使用HTML字符串替换而不是直接操作Document模型
- 如果需要更复杂的样式控制,考虑使用
JTextPane而不是JEditorPane
完整示例:
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
public class CompleteCenteredExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("完整居中示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setContentType("text/html");
editorPane.setEditable(false);
// 完整的居中HTML
String html = "<html><head><style>" +
"body { font-family: Arial; padding: 20px; }" +
".center { text-align: center; }" +
".container { width: 100%; }" +
"</style></head>" +
"<body>" +
"<div class='container'>" +
"<h1 class='center' style='color: #333;'>居中标题</h1>" +
"<p class='center' style='font-size: 18px;'>这是居中的段落文本</p>" +
"<center>" +
"<table border='1' cellpadding='10'>" +
"<tr><td>居中表格</td></tr>" +
"</table>" +
"</center>" +
"<p style='text-align: center; color: #666;'>使用CSS居中的文本</p>" +
"</div>" +
"</body></html>";
editorPane.setText(html);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane);
frame.setVisible(true);
});
}
}
这些方法都可以在 JEditorPane 中实现文本居中的效果,选择哪种方法取决于你的具体需求和使用场景。