请,有人可以告诉我如何禁用的文本高亮显示JTextPane
。我JTextPane
是半透明的,因此突出了越野车。但是没有突出显示是好的!
我尝试了以下方法:
DefaultHighlighter highlighter = (DefaultHighlighter) chatTextPane.getHighlighter();
highlighter.removeAllHighlights();
chatTextPane.setHighlighter(null);
chatTextPane.setSelectedTextColor(new Color(0,0,0,0));
chatTextPane.setSelectionColor(new Color(0,0,0,0));
chatTextPane.setSelectionStart(0);
chatTextPane.setSelectionEnd(0);
chatTextPane.setCaret(new NoTextSelectionCaret(chatTextPane));
// with:
private class NoTextSelectionCaret extends DefaultCaret
{
public NoTextSelectionCaret(JTextComponent textComponent)
{
setBlinkRate( textComponent.getCaret().getBlinkRate() );
textComponent.setHighlighter( null );
}
@Override
public int getMark()
{
return getDot();
}
}
并用的东西highlighter.getDrawsLayeredHighlights();
,以及,我也不记得了。
谢谢!
由于Swing的工作方式,高亮显示了一些不良的伪像,我将JTextPane包裹在另一个透明颜色的面板中。
消除工件,将JTextPane包装在AlphaContainer中。
public class AlphaContainer extends JComponent
{
private JComponent component;
public AlphaContainer(JComponent component)
{
this.component = component;
setLayout( new BorderLayout() );
setOpaque( false );
component.setOpaque( false );
add( component );
}
/**
* Paint the background using the background Color of the
* contained component
*/
@Override
public void paintComponent(Graphics g)
{
g.setColor( component.getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
}
}
然后:
AlphaContainer ac = new AlphaContainer(chatTextPane);
并将AlphaContainer添加到您的框架中。如果要将AlphaContainer包含在另一个组件中,请将另一个组件设置为setOpaque(false);
。
然后,您可以chatTextPane.setHighlighter(null);
根据需要设置禁用突出显示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句