在jTextPane中更改所选文本的颜色

我正在使用JTextPane创建一个文本编辑器,允许用户更改所选文本的颜色。 但是当用户选择文本时,则选择更改颜色(例如,红色)的选项,文本不会显示为红色,直到文本被取消选中。 我尝试使用setSelectedTextColor来更改所选文本的颜色,但这不起作用,因为无论何时随后选择文本,都会将文本更改为红色。 有没有办法让选定的文字显示为实际颜色? 或者喜欢它在Word中的工作方式,它不是文本的实际颜色,但是当选择不同颜色的文本时,即使选择它们也会显示为不同的颜色。

我使用以下代码设置JTextPane和按钮,将所选文本更改为红色:

JButton redButton = new JButton(new StyledEditorKit.ForegroundAction("red", Color.RED)); redButton.setFocusable(false); buttonPanel.add(redButton); 

JTextPane设置为内容类型HTML并使用HTMLEditorKit:

 p=new JTextPane(); p.setSize(300, 300); kit = new HTMLEditorKit(); p.setEditorKit(kit); p.setDocument(kit.createDefaultDocument()); p.setContentType("text/html"); p.setEditable(true); 

如果您需要更多源代码来理解这个问题,请告诉我。 谢谢!

听起来你可能正在使用字体系列名称以外的东西。 我重新考虑了这个例子 ,使用JTextPane并看到了预期的结果。 如上所述,这些操作需要一个字体系列名称,例如default或face=SansSerif ,由嵌套在StyledEditorKitFontFamilyAction类指定。

图片

 JTextPane textPane = new JTextPane(); 

看一下DefaultHighlightPainter内部类。

方法

  public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) { Rectangle alloc = bounds.getBounds(); try { // --- determine locations --- TextUI mapper = c.getUI(); Rectangle p0 = mapper.modelToView(c, offs0); Rectangle p1 = mapper.modelToView(c, offs1); // --- render --- Color color = getColor(); if (color == null) { g.setColor(c.getSelectionColor()); } else { g.setColor(color); } 

如您所见,它使用getColor()getSelectionColor() 。 您可以扩展课程并调整高光绘画。

或者使用更简单的方法来覆盖你的JTextPanegetSelectionColor() 。 在该方法中,仅检查是否选择了文本并使用所选元素的属性来获得所需的ccolor。 如果没有选择任何内容,则返回super.getSelectedColor()

更新:实际应用颜色进行选择用于低级GlyphView的公共无效油漆(图形g,形状a){… JTextComponent tc =(JTextComponent)c; 颜色selFG = tc.getSelectedTextColor();

  if (// there's a highlighter (bug 4532590), and (tc.getHighlighter() != null) && // selected text color is different from regular foreground (selFG != null) && !selFG.equals(fg)) { Highlighter.Highlight[] h = tc.getHighlighter().getHighlights(); if(h.length != 0) { boolean initialized = false; int viewSelectionCount = 0; for (int i = 0; i < h.length; i++) { Highlighter.Highlight highlight = h[i]; int hStart = highlight.getStartOffset(); int hEnd = highlight.getEndOffset(); if (hStart > p1 || hEnd < p0) { // the selection is out of this view continue; } if (!SwingUtilities2.useSelectedTextColor(highlight, tc)) { continue; } 

...

如您所见,应用选择颜色与视图的默认颜色在SwingUtilities2.useSelectedTextColor(highlight,tc)中定义

在来源http://kickjava.com/src/com/sun/java/swing/SwingUtilities2.java.htm

  public static boolean useSelectedTextColor(Highlighter.Highlight JavaDoc h, JTextComponent JavaDoc c) { Highlighter.HighlightPainter JavaDoc painter = h.getPainter(); String JavaDoc painterClass = painter.getClass().getName(); if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 && painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) { return false; } try { DefaultHighlighter.DefaultHighlightPainter JavaDoc defPainter = (DefaultHighlighter.DefaultHighlightPainter JavaDoc) painter; if (defPainter.getColor() != null && !defPainter.getColor().equals(c.getSelectionColor())) { return false; } } catch (ClassCastException JavaDoc e) { return false; } return true; } 

所以使用颜色取决于L&F和画家。 如果您定义了您的onw画家,则不会使用该颜色。

更改所选文本颜色的最简单方法:

 int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); int selectedLength = end - start; StyleDocument style = pane.getStyledDocument(); //this give your attribute set of selected Text. AttributeSet oldSet = style.getCharacterElement(end-1).getAttributes(); //StyleContext for creating attribute set StyleContext sc = StyleContext.getDefaultStyleContext(); // Attribute set which contains new color with old attributes AttributeSet s = sc.addAttribute(oldSet, StyleConstants.Foreground, Color.RED); //This set the color of the Text style.setCharacterAttributes(start, selectedLength, s, true);