html编辑器怎么改变颜色,如何为改变文本部分的HTML编辑器的背景颜色与一个HTMLEditorKit内置提供Functionalty...

我想使我的小HTML编辑器的用户对 不同的背景颜色之间进行切换正在输入文字。我第一次尝试 为此目的使用CSS样式。不同的风格定义了 不同的背景颜色,并且通过JComboBox用户可以在这些风格之间切换 。一旦在HTMLDocument中选择 各自位置的样式,将会输入 类型的新HTML元素。 不幸的是我无法完成这项工作。量程元素 根本就没有创建(see my question regarding this problem)。

在之间我看看类StyledEditorKit.ForegroundAction 了解如何运作。执行后,它只是修改StyledEditorKit的 输入属性,使用前景颜色设置新的 。之后输入的文字将以 显示新的前景色。当将HTML代码写入文件时,该文本自动包含在HTML元素中。而且所有这些都可以运行在选定的文本上,这些文本可能会在多个段落上运行 。在这种情况下,显然受影响的 文本在所有受影响的段落中都包含在 HTML标记中。

我想完成同样的功能,用于在任意文本块上设置背景颜色 。但出乎意料的是这似乎并不 那么容易:-(

我没有找到一个预定义的动作类类似于Java 7的JDK的 StyledEditorKit.foregroundAction这一目的。创建 这样一类似乎没有是复杂的,它几乎一样 ForegroundAction与actionPerformed方法改为设置 背景而不是前景属性的

但是如何建立有效的HTML代码,设置一个特定的背景 颜色所包含的部分。文字? 直到现在我不知道哪个部分HTMLEditorKit执行 创建所有元素的HTMLDocument中的文本 具有设置的前景属性。我认为,从现有的代码 创建元素应该不会太难得到的 实现创建 元素,而不是用于设置背景颜色 文本的任意区域元素。或者,这一切已经可用,我只有没有注意到?任何帮助,将不胜感激!

在我之间,我向前迈出了重要的一步,并感谢找到的一段代码here 我设法创建了有效的元素。在span元素中,我使用class属性来分配预定义样式。

这里是我的代码:

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.Action;

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.BadLocationException;

import javax.swing.text.MutableAttributeSet;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.Style;

import javax.swing.text.StyleContext;

import javax.swing.text.html.HTML;

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) throws BadLocationException, IOException {

final SimpleEditor editor = new SimpleEditor();

editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

editor.setVisible(true);

}

public SimpleEditor() throws BadLocationException, IOException {

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 styleNames = new Vector();

final Enumeration> names = predefStyles.getStyleNames();

while (names.hasMoreElements()) {

styleNames.add((String) names.nextElement());

}

final DefaultComboBoxModel stylesModel =

new DefaultComboBoxModel(styleNames);

final JComboBox cbStyleSel = new JComboBox(stylesModel);

final JToolBar bar = new JToolBar();

Action dumpAction = null;

for (final Action act : edtKit.getActions()) {

if (act.getValue(Action.NAME).equals("dump-model")) {

dumpAction = act;

break;

}

}

bar.add(dumpAction);

cbStyleSel.setEditable(false);

cbStyleSel.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

e.getSource();

@SuppressWarnings("unchecked")

final JComboBox cboStyleSel = (JComboBox) e.getSource();

final String selItem = (String) cboStyleSel.getSelectedItem();

final MutableAttributeSet divAttributes = new SimpleAttributeSet();

if (selItem.equals("default")) {

// This does not work!

final Style defStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

divAttributes.addAttribute(HTML.Tag.CONTENT, defStyle);

textPane.setCharacterAttributes(divAttributes, true);

} else {

divAttributes.addAttribute(HTML.Attribute.CLASS, selItem.substring(1));

final MutableAttributeSet tagAttributes = new SimpleAttributeSet();

tagAttributes.addAttribute(HTML.Tag.SPAN, divAttributes);

textPane.setCharacterAttributes(tagAttributes, false);

}

textPane.requestFocusInWindow();

}

});

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 SaveAction actSave = new SaveAction();

mnuFile.add(actSave);

return menubar;

}

class SaveAction extends AbstractAction {

private static final long serialVersionUID = 1L;

public SaveAction() {

super("Save");

}

@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) {

}

}

}

}

}

}

到目前为止好!这个解决方案唯一的问题是,我无法实现从包含在元素中的文本切换回“正常”文本,即文本不在元素内。 这应该没什么大不了的,但不幸的是我无法弄清楚我该如何实现这一点。任何想法都会非常受欢迎!

2013-07-24

apatwork

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值