油漆的地方不应该是

时间:2017-04-22 18:45:16

标签: java swing jtabbedpane

我目前正在撰写自定义BasicTabbedPaneUI课程,但是,当JTabbedPane's标签展示位置为JTabbedPane.RIGHT时,我遇到了问题。如此设置时,标签上方和下方都有背景。据我所知,我的代码并不意味着这样做。

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

Top is <code>JTabbedPane.TOP</code> and bottom is <code>JTabbedPane.RIGHT</code>
Top是JTabbedPane.TOP,Bottom是JTabbedPane.RIGHT
我的期望是什么。

This is what I expected
Top是JTabbedPane.TOP,Bottom是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) {}
}

1 个答案:

答案 0 :(得分:2)

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);
}

看起来你错过了&#34;休息;&#34;声明所以默认的绘画代码也正在执行。

代码应该是:

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