在Java编程中,实现一个功能丰富的富文本框可以帮助用户进行更加灵活和高效的文本编辑。Java Swing库提供了JTextPane组件,它是一个强大的文本编辑器,可以轻松实现富文本编辑功能。下面,我将详细介绍如何使用Java编写一个简单的富文本框,并实现一些基本的文本编辑功能。
1. 创建富文本框
首先,我们需要创建一个JFrame窗口,并在其中添加一个JTextPane组件作为富文本框。
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class RichTextEditor extends JFrame {
private JTextPane textPane;
public RichTextEditor() {
super("富文本编辑器");
// 创建富文本框
textPane = new JTextPane();
textPane.setMargin(new Insets(5, 5, 5, 5));
// 设置富文本框的文档
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), StyleConstants.setAlignment(StyleConstants.ALIGN_LEFT), false);
// 将富文本框添加到窗口
add(new JScrollPane(textPane), BorderLayout.CENTER);
// 设置窗口属性
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new RichTextEditor().setVisible(true);
});
}
}
2. 设置文本样式
JTextPane组件支持多种文本样式,如字体、颜色、大小等。我们可以通过Style类来设置这些样式。
import javax.swing.text.*;
public class RichTextEditor extends JFrame {
// ...(其他代码)
public void setFontStyle(String fontName, int fontSize, Color color) {
Style style = textPane.getStyledDocument().addStyle("MyStyle", null);
StyleConstants.setFontFamily(style, fontName);
StyleConstants.setFontSize(style, fontSize);
StyleConstants.setForeGround(style, color);
// 设置当前选中文本的样式
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
textPane.getStyledDocument().setCharacterAttributes(start, end - start, style, false);
}
}
3. 插入图片
在富文本框中插入图片,我们可以使用Image类和Style类来实现。
import javax.swing.text.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class RichTextEditor extends JFrame {
// ...(其他代码)
public void insertImage(String imagePath) {
try {
BufferedImage image = ImageIO.read(new File(imagePath));
Image scaledImage = image.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
// 创建图片风格
Style style = textPane.getStyledDocument().addStyle("ImageStyle", null);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
StyleConstants.setComponent(style, new ImageIcon(scaledImage));
// 插入图片
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
textPane.getStyledDocument().insertString(start, "\n", null);
textPane.getStyledDocument().insertString(start, "\t", style);
textPane.getStyledDocument().insertString(start + 1, "\n", null);
textPane.getStyledDocument().setCharacterAttributes(start + 1, 1, style, false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 保存和加载文档
为了方便用户保存和加载富文本框中的内容,我们可以使用File类和StyledDocument类来实现。
import javax.swing.filechooser.FileNameExtensionFilter;
public class RichTextEditor extends JFrame {
// ...(其他代码)
public void saveDocument(String filePath) {
try {
StyledDocument doc = textPane.getStyledDocument();
File file = new File(filePath);
FileOutputStream fos = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
String text = doc.getText(0, doc.getLength());
writer.write(text);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadDocument(String filePath) {
try {
StyledDocument doc = textPane.getStyledDocument();
doc.remove(0, doc.getLength());
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
doc.insertString(doc.getLength(), line + "\n", null);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过以上步骤,我们可以轻松地使用Java编写一个功能丰富的富文本框,并实现文本编辑新体验。在实际应用中,可以根据需求进一步完善和扩展富文本框的功能。
