【发布时间】:2011-11-29 06:27:48
【问题描述】:
我刚刚遇到了一件有趣的事情。
我正在更改选定的文本样式。问题是,当我一个一个地更改 ONE WORD 的样式时,这很好,但是接下来如果我选择一个完整样式的短语并更改其字体颜色,整个短语将变为 ONE 样式(所选文本中的第一个样式 ) 仅 :(
这里是sn-p的问题
private void setFontColorStyle()
{
JTextPane editor=this.getTextPane();
String text=this.getTextPane().getSelectedText();
StyledDocument doc=(StyledDocument) editor.getDocument();
int selectionEnd=this.getTextPane().getSelectionEnd();
int selectionStart=this.getTextPane().getSelectionStart();
Element element=doc.getCharacterElement(selectionStart);
AttributeSet as = element.getAttributes();
String family = StyleConstants.getFontFamily(as);
int fontSize = StyleConstants.getFontSize(as);
boolean isBold=StyleConstants.isBold(as);
boolean isItalic=StyleConstants.isItalic(as);
boolean isUnderlined=StyleConstants.isUnderline(as);
StyleContext context = new StyleContext();
Style style;
this.getTextPane().replaceSelection("");
style = context.addStyle("mystyle", null);
style.addAttribute(StyleConstants.FontSize, fontSize);
style.addAttribute(StyleConstants.FontFamily, family);
style.addAttribute(StyleConstants.Foreground, this.fontColor);
style.addAttribute(StyleConstants.Bold, isBold);
style.addAttribute(StyleConstants.Italic, isItalic);
style.addAttribute(StyleConstants.Underline, isUnderlined);
this.getTextPane().replaceSelection("");
try {
this.getTextPane().getStyledDocument().insertString(selectionEnd - text.length(), text, style);
} catch (BadLocationException ex) {
}
}
这里是加粗的制作方法代码... () 斜体和下划线都是相同的逻辑,所以我想它很清楚
private void setFontBoldStyle()
{
if(this.getTextPane().getSelectedText()!=null)
{
String text = this.getTextPane().getSelectedText();
int selectionStart=this.getTextPane().getSelectionStart();
int selectionEnd=this.getTextPane().getSelectionEnd();
StyleContext context = new StyleContext();
Style style;
Element element=doc.getCharacterElement(selectionStart);
Enumeration en=doc.getStyleNames();
AttributeSet as = element.getAttributes();
/**
* Get style from history...
*/
String family = StyleConstants.getFontFamily(as);
int fontSize = StyleConstants.getFontSize(as);
Color currentColor=StyleConstants.getForeground(as);
boolean isBold=StyleConstants.isBold(as)?false:true;
boolean isItalic=StyleConstants.isItalic(as);
boolean isUnderlined=StyleConstants.isUnderline(as);
String styleName=String.valueOf(Math.random());
style = context.addStyle(styleName, null);
// style.addAttribute(StyleConstants.FontSize, fontSize);
// style.addAttribute(StyleConstants.FontFamily, family);
style.addAttribute(StyleConstants.Foreground, currentColor);
style.addAttribute(StyleConstants.FontFamily, family);
style.addAttribute(StyleConstants.FontSize, fontSize);
style.addAttribute(StyleConstants.Bold, isBold);
style.addAttribute(StyleConstants.Italic, isItalic);
style.addAttribute(StyleConstants.Underline, isUnderlined);
this.getTextPane().replaceSelection("");
try {
this.getTextPane().getStyledDocument().insertString(selectionEnd - text.length(), text, style);
} catch (BadLocationException ex) {
}
}//if end...
}
下面是粗体的方法调用代码:
private void setFontBold()
{
this.setFontBoldStyle();
}
...和颜色方法调用
private void setFontColor(Color fontColor)
{
this.fontColor=fontColor;
this.setFontColorStyle();
}
...和动作监听器(粗体)...
private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) {
this.getTextPane().requestFocusInWindow();
this.setFontBold();
}
...对于颜色
private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) {
this.getTextPane().requestFocusInWindow();
ColorDialog colorEditor=new ColorDialog();
//returns rgb color...
Color color=colorEditor.getSelectedColor(this.getDialog(), true,false);
if(color==null){
JOptionPane.showMessageDialog(this.getDialog(), "null color");
return;
}
this.setFontColor(color);
}
例如,当我想更改完全不同样式的选定文本颜色时,我非常需要您关于如何保持选定文本样式不变(如粗体或字体系列)的建议?
为了更清楚...
例如我有文字
我的 你好 世界不漂亮:)
接下来我选择整个短语并将其颜色从黑色更改为红色。下一个文本变为红色,但整个短语根据第一种样式变为粗体。但问题是保持粗体和斜体样式但同时使用红色短语会很有趣:) 非常简单但我只是混淆了如何在所选文本区域的框架中控制多个样式?
非常感谢任何有用的评论
【问题讨论】: