如何完全禁用jtextpane的文本突出显示?

r6l8ljro  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(269)

请问,有人能告诉我如何禁用文本突出显示 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(); 我也不记得了。
谢谢!

0ve6wy6x

0ve6wy6x1#

由于swing的工作方式,高亮显示显示了一些不需要的工件,我将jtextpane Package 在另一个透明的面板中。
为了消除工件,将jtextpane Package 在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要包含在另一个组件中,请将另一个组件设置为 setOpaque(false); .
然后你可以设置 chatTextPane.setHighlighter(null); 如果需要,禁用突出显示。
有关更多详细信息,请参阅camickr提供的透明背景

相关问题