JTabbedPane选项卡边框删除

时间:2015-09-23 08:21:55

标签: java swing jtabbedpane

JTabbedPane - 即使在使用BasicTabbedPaneUI之后也会显示制表符边框。请让我知道如何更改标签之间没有边框?有没有办法彻底删除边框?

enter image description here

请将代码段发送给我。提前谢谢。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TabbedPaneTest implements Runnable {

    @Override
    public void run() {
        final JTabbedPane pane = new JTabbedPane();
        pane.addTab("A", createTab(Color.RED));
        pane.addTab("B", createTab(Color.YELLOW));
        pane.addTab("C", createTab(Color.BLUE));

        pane.setUI(new BasicTabbedPaneUI() {
            @Override
            protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {

            }
        });
        pane.setBackground(Color.WHITE);

        pane.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent arg0) {
                pane.setForegroundAt(pane.getSelectedIndex(), Color.GREEN);

            }
        });
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        //frame.setSize(500, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createTab(Color color) {
        JPanel p = new JPanel(new BorderLayout()) {
            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        p.setBorder(BorderFactory.createLineBorder(color, 2));
        p.setBackground(Color.WHITE);
        return p;
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new TabbedPaneTest());
    }
}

1 个答案:

答案 0 :(得分:1)

// Remove the white line under a tab button
Insets insets = UIManager.getInsets("TabbedPane.contentBorderInsets");
insets.top = -1;
UIManager.put("TabbedPane.contentBorderInsets", insets);