gpt4 book ai didi

java - 样式文本未从 JTextPane 正确保存

转载 作者:行者123 更新时间:2023-12-01 04:30:33 24 4
gpt4 key购买 nike

我正在尝试实现一个小型 HTML 编辑器,允许将前景色和背景色应用于部分文本。我想以 HTML 格式存储文本,并将颜色信息保留在文件中。当使用不同样式(样式定义前景色和背景色信息)编辑文本时,我当前的实现可以正常运行。但是,当将编辑后的文档存储在文件中时,我得到的 HTML 结构不正确,并且使用特定样式输入的文本部分丢失。这是我的代码:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.text.AttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class SimpleEditor extends JFrame {

private static final long serialVersionUID = 1L;
private final JTextPane textPane;
private final HTMLEditorKit edtKit;
private final HTMLDocument doc;
private final StyleSheet predefStyles;

public static void main(String[] args) {
final SimpleEditor editor = new SimpleEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}

public SimpleEditor() {
super("Very Simple HTML Editor");
textPane = new JTextPane();
edtKit = new HTMLEditorKit();
textPane.setEditorKit(edtKit);
predefStyles = new StyleSheet();
predefStyles.addRule("MyStyle1 { color:#cc0000; background-color:silver }\n" +
"MyStyle2 { color:#0000cc; background-color:aqua }");
doc = new HTMLDocument(predefStyles);
textPane.setDocument(doc);

final Container content = getContentPane();
content.add(textPane, BorderLayout.CENTER);
content.add(createToolBar(), BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(500, 240);
}

private JToolBar createToolBar() {
final Vector<String> styleNames = new Vector<String>();
final Enumeration<?> names = predefStyles.getStyleNames();
while (names.hasMoreElements()) {
styleNames.add((String) names.nextElement());
}
final DefaultComboBoxModel<String> stylesModel =
new DefaultComboBoxModel<String>(styleNames);
final JComboBox<String> cbStyleSel = new JComboBox<String>(stylesModel);
final JToolBar bar = new JToolBar();
cbStyleSel.setEditable(false);
cbStyleSel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
e.getSource();
@SuppressWarnings("unchecked")
final JComboBox<CondStyle> cboStyleSel =
(JComboBox<CondStyle>) e.getSource();
final String selItem = (String) cboStyleSel.getSelectedItem();
final Style style = textPane.getStyle(selItem);
textPane.setCharacterAttributes(extractStyleAttribs(style), true);
SimpleEditor.this.requestFocus();
}
});
bar.add(cbStyleSel);
return bar;
}

/**
* Extracts the style attributes except the style's name
* @param aStyle The style to be processed
* @return The visual attributes extracted from the style
*/
AttributeSet extractStyleAttribs(Style aStyle) {
final MutableAttributeSet attribs = new SimpleAttributeSet();
final Enumeration<?> attribNames = aStyle.getAttributeNames();
while (attribNames.hasMoreElements()) {
final Object attribName = attribNames.nextElement();
if (attribName == Style.NameAttribute) {
continue;
}
attribs.addAttribute(attribName, aStyle.getAttribute(attribName));
}
return attribs;
}

private JMenuBar createMenuBar() {
final JMenuBar menubar = new JMenuBar();
final JMenu mnuFile = new JMenu("File");
menubar.add(mnuFile);
final ExitAction actSave = new ExitAction();
mnuFile.add(actSave);
return menubar;
}

class ExitAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public ExitAction() {
super("Save & Exit");
}
@Override
public void actionPerformed(ActionEvent ev) {
final JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(SimpleEditor.this) !=
JFileChooser.APPROVE_OPTION)
return;
final File file = chooser.getSelectedFile();
if (file == null)
return;
FileWriter writer = null;
try {
writer = new FileWriter(file);
textPane.write(writer);
} catch (final IOException ex) {
JOptionPane.showMessageDialog(SimpleEditor.this,
"File Not Saved", "ERROR",
JOptionPane.ERROR_MESSAGE);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final IOException x) {
}
}
}
System.exit(0);
}
}
}

这是应用程序的屏幕截图,其中输入了一些样式文本: The application with some colored text

保存文档 ( textPane.write(writer); ) 时创建的 HTML 文件结构不正确。 <body> ... </body>部分如下所示:

<body>
<p style="margin-top: 0">
default text
</p>
<p style="margin-top: 0">
<font color="#cc0000"><p style="background-color: silver">
</font> </p>
<p style="margin-top: 0">
<font color="#0000cc"><p style="background-color: aqua">
</font> </p>
<p style="margin-top: 0">
<font color="#333333" face="Dialog" size="3"><p FONT_ATTRIBUTE_KEY="javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12]">
</font> </p>
</body>

很明显,生成的 HTML 代码不正确。里面<p>元素一个新<font>创建的元素定义了样式的前景色。然后另一个<p>创建具有背景颜色定义的元素。但文字不见了。然后而不是关闭 </p>标记结束 </font>标签后面是 </p>标签关闭外部 <p>元素。我究竟做错了什么?

最佳答案

经过大量研究,我找到了解决问题的简单方法。请参阅here .

关于java - 样式文本未从 JTextPane 正确保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18016402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com