画不该画的地方

Paint where there shouldn't be

我目前正在编写自定义 BasicTabbedPaneUI class,但是,当 JTabbedPane's 选项卡位置为 JTabbedPane.RIGHT 时,我遇到了问题。这样设置后,标签的上下都有背景。据我所知,我的代码并不打算这样做。

这可以在下图中清楚地看到。


顶部是 JTabbedPane.TOP,底部是 JTabbedPane.RIGHT
我所期望的是。


顶部是 JTabbedPane.TOP,底部是 JTabbedPane.RIGHT
如果您能帮助我解决问题,我将不胜感激。


GUI.java

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

@SuppressWarnings("serial")
public class GUI extends JFrame {

    JTabbedPane tabs = new JTabbedPane();
    //JTabbedPane tabs = new JTabbedPane(JTabbedPane.RIGHT);

    private GUI() {
        super("GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());
        createLayout();
        add(tabs);

        pack();
        setLocationRelativeTo(null);

        setVisible(true);
    }

    private void createLayout() {
        tabs.setPreferredSize(new Dimension(600, 200));
        tabs.setUI(new TabsUI());

        JPanel p1 = new JPanel();
        p1.add(new JLabel("Panel 1"));
        tabs.addTab("Panel 1", p1);

        JPanel p2 = new JPanel();
        p2.add(new JLabel("Panel 2"));
        tabs.addTab("Panel 2", p2);

        JPanel p3 = new JPanel();
        p3.add(new JLabel("Panel 3"));
        tabs.addTab("Panel 3", p3);

        JPanel p4 = new JPanel();
        p4.add(new JLabel("Panel 4"));
        tabs.addTab("Panel 4", p4);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new GUI());
    }
}

TabsUI.java

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TabsUI extends BasicTabbedPaneUI{
    public static ComponentUI createUI(JComponent c) {
        return new TabsUI();
    }

    int wSep = 10;
    int hSep = 10;

    @Override
    protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        int greyScale = 150;
        g.setColor(new Color(greyScale, greyScale, greyScale));

        switch(tabPlacement) {
            case LEFT:
            case RIGHT:
                g.fillRect(x, y + (hSep / 2), w, h - hSep);
            case BOTTOM:
            case TOP:
            default:
                g.fillRect(x + (wSep / 2), y, w - wSep, h);
        }
    }

    @Override
    protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
        if (tabPlacement == LEFT || tabPlacement == RIGHT) {
            return 50 + super.calculateTabWidth(tabPlacement, tabIndex, metrics);
        } else {
            return 50 + (wSep * 2) + super.calculateTabWidth(tabPlacement, tabIndex, metrics);
        }
    }

    @Override
    protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
        if (tabPlacement == LEFT || tabPlacement == RIGHT) {
            return 5 + hSep + super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);
        } else {
            return 5 + super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);
        }
    }

    @Override
    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {}

    @Override
    protected void paintFocusIndicator(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect, boolean isSelected) {}
}
switch(tabPlacement) {
    case LEFT:
    case RIGHT:
        g.fillRect(x, y + (hSep / 2), w, h - hSep);
    case BOTTOM:
    case TOP:
    default:
        g.fillRect(x + (wSep / 2), y, w - wSep, h);
}

看起来您缺少 "break;" 语句,因此默认绘画代码也正在执行。

代码应该是:

        g.fillRect(x, y + (hSep / 2), w, h - hSep);
        break;