BasicTabbedPaneUI聚焦时显示边框

时间:2018-11-15 02:55:40

标签: java graphics

我正在尝试通过扩展BasicTabbedPaneUI

来创建自定义TabbedPaneUI

我在设置TextColor时遇到问题,每当设置TextColor时,当窗格被聚焦并启用时,它将显示边框。

enter image description here

如您所见,具有标题Test的窗格已启用/已选择窗格,并且其周围有白色边框,我不希望使用该边框。我已经覆盖了paintTabBorder

@Override
protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
    //We do nothing here to disable painting the Tab Border
}

这是我如何覆盖paintText来更改文本颜色

@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {

    g.setFont(font);

    View v = getTextViewForTab(tabIndex);
    if (v != null) {
        // html
        v.paint(g, textRect);
    } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);

        if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {

            //Change the text to White here <- Causing Border Issues
            g.setColor(Color.WHITE);

            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x, textRect.y + metrics.getAscent());

        } else { // tab disabled
            g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x, textRect.y + metrics.getAscent());
            g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x - 1, textRect.y + metrics.getAscent() - 1);

        }
    }
}

g.setColor(Color.WHITE);引起了边框问题,我已经通过将“颜色”设置为其他颜色而不是“白色”来确认了这一点,边框和文本一样都是该颜色。

2 个答案:

答案 0 :(得分:0)

只需用背景颜色绘制另一个空标题即可有效隐藏边框。

@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {

    g.setFont(font);

    View v = getTextViewForTab(tabIndex);
    if (v != null) {
        // html
        v.paint(g, textRect);
    } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);

        if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {

            //Change the text to White here
            g.setColor(Color.WHITE);

            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x, textRect.y + metrics.getAscent());

            //Remove Border Issues
            g.setColor(tabPane.getBackgroundAt(tabIndex).darker());

            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    "", mnemIndex,                                 //<-- empty title here
                    textRect.x, textRect.y + metrics.getAscent());

        } else { // tab disabled
            g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x, textRect.y + metrics.getAscent());
            g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x - 1, textRect.y + metrics.getAscent() - 1);

        }
    }
}

答案 1 :(得分:0)

经过一些研究,我想出了解决边框问题的方法,我必须在绘制String后设置图形颜色。 g.setColor(selectColor);

            //Change the text to White here <- Causing Border Issues
            g.setColor(Color.WHITE);

            SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
                    title, mnemIndex,
                    textRect.x, textRect.y + metrics.getAscent());

            //reset the color to selectedColor
            g.setColor(selectColor);