Java 如何在使用HTMLEditorKit构建的HTML编辑器中提供更改文本部分背景颜色的功能
我的问题如下:Java 如何在使用HTMLEditorKit构建的HTML编辑器中提供更改文本部分背景颜色的功能,java,swing,htmleditorkit,Java,Swing,Htmleditorkit,我的问题如下: edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN); cbStyleSel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { e.getSource(); @SuppressWa
edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN);
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();
if (selItem.equals("default")) {
edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN);
} else {
final MutableAttributeSet divAttributes = new SimpleAttributeSet();
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();
}
});
我想让我的小HTML编辑器的用户能够在
输入文本的不同背景色。我第一次试过
为此目的使用CSS样式。不同的样式定义
不同的背景颜色,通过JComboBox
用户可以
在这些样式之间切换。在中选择样式时
HTMLDocument
类型的新HTML元素中的相应位置
将输入
。
不幸的是,我无法得到这份工作。跨度元素
只是没有创建()
在此期间,我查看了类StyledEditorKit.ForegroundAction
来了解它是如何工作的。在执行时,它只是修改
正在使用的StyledEditorKit
的输入属性设置一个新的
前景色。随后输入的文本将显示为
新的前景色。在将HTML代码写入文件时,
文本自动包含在中代码>
HTML元素。所有这些甚至都适用于选定的文本,可能
浏览多个段落。在这种情况下,显然受影响的
所有受影响段落中的文本都包含在
HTML标记中
我想完成设置背景的相同功能
任意文本块上的颜色。但令人惊讶的是,这似乎并非如此
这么简单:-(
我没有找到类似于
Java7JDK.Creating中的StyledEditorKit.foregroundAction
这样的类似乎并不复杂;它几乎与
ForegroundAction
,将actionPerformed
方法更改为设置
背景而不是前景属性
但是如何创建设置特定背景的有效HTML代码呢
所包含文本部分的颜色?
直到现在,我还不知道HTMLEditorKit
的哪个部分执行
为HTMLDocument
中的文本创建所有
元素
设置了前台属性。我认为从现有代码
创建
元素派生
创建
用于设置背景色的元素,而不是
元素
对于任意的文本区域。或者所有这些都已经可用并且我
只是没有注意到?如果有任何帮助,我们将不胜感激
在这两者之间,我向前迈出了重要的一步,这要感谢找到的一段代码
我设法创建了有效的
元素。在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<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();
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<CondStyle> cboStyleSel = (JComboBox<CondStyle>) 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) {
}
}
}
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Enumeration;
导入java.util.Vector;
导入javax.swing.AbstractAction;
导入javax.swing.Action;
导入javax.swing.DefaultComboxModel;
导入javax.swing.JComboBox;
导入javax.swing.JFileChooser;
导入javax.swing.JFrame;
导入javax.swing.JMenu;
导入javax.swing.JMenuBar;
导入javax.swing.JOptionPane;
导入javax.swing.JTextPane;
导入javax.swing.JToolBar;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.MutableAttributeSet;
导入javax.swing.text.SimpleAttributeSet;
导入javax.swing.text.Style;
导入javax.swing.text.StyleContext;
导入javax.swing.text.html.html;
导入javax.swing.text.html.HTMLDocument;
导入javax.swing.text.html.HTMLEditorKit;
导入javax.swing.text.html.StyleSheet;
公共类SimpleEditor扩展JFrame{
私有静态最终长serialVersionUID=1L;
私有最终JTextPane文本窗格;
私人最终HTMLEditorKit edtKit;
私人最终HTMLDocument文件;
私有最终样式表预定义样式;
公共静态void main(字符串[]args)引发BadLocationException、IOException{
最终SimpleEditor=新SimpleEditor();
编辑器.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
公共SimpleEditor()引发BadLocationException、IOException{
超级(“非常简单的HTML编辑器”);
textPane=新的JTextPane();
edtKit=新的HTMLEditorKit();
textPane.setEditorKit(edtKit);
predefStyles=新样式表();
predefStyles.addRule(“.MyStyle1{color:#cc0000;背景色:银色}\n”+
“.MyStyle2{color:#0000cc;背景色:aqua}”);
doc=新的HTMLDocument(预定义样式);
textPane.setDocument(doc);
最终容器内容=getContentPane();
添加(文本窗格,BorderLayout.CENTER);
添加(createToolBar(),BorderLayout.NORTH);
setJMenuBar(createMenuBar());
设置大小(500240);
}
私有JToolBar createToolBar(){
最终向量样式名=新向量();
最终枚举名称=predefStyles.getStyleNames();
while(names.hasMoreElements()){
添加((字符串)name.nextElement());
}
最终的DefaultComboxModel样式模型=
新的DefaultComboxModel(样式名);
最终JCOMBOX cbStyleSel=新JCOMBOX(样式模型);
最终JToolBar=新的JToolBar();
Action dumpAction=null;
对于(最终行动:edtKit.getActions()){
if(act.getValue(Action.NAME).equals(“转储模型”)){
行动=行动;
打破
}
}
添加(转储操作);
cbStyleSel.setEditable(false);
cbStyleSel.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){