如何删除JTabbedPane Tab上边框/阴影?

时间:2017-03-19 01:58:55

标签: java swing jtabbedpane

package javaapplication22;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;

public class NewJFrame extends javax.swing.JFrame {

    class TextPane extends JTextPane {

        public TextPane() {

            super();
            this.setOpaque(false);
        }

        public void paintComponent(Graphics g) {

            g.setColor(this.getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());

            super.paintComponent(g);
        }

    }

    class TransparentScrollPane extends JScrollPane {

        public TransparentScrollPane(Component view) {

            super(view);
            this.setOpaque(false);
            this.viewport.setOpaque(false);
            this.setBorder(null);
        }
    }

    class AlphaContainer extends JComponent {

        private JComponent component;

        public AlphaContainer(JComponent component) {

            this.component = component;
            setLayout(new BorderLayout());
            setOpaque(false);
            component.setOpaque(false);
            add(component);
        }

        @Override
        public void paintComponent(Graphics g) {
            g.setColor(component.getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    public NewJFrame() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        UIManager.put("TabbedPane.contentOpaque", false);
        UIManager.getDefaults().put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
        UIManager.getDefaults().put("TabbedPane.tabsOverlapBorder", false);
        UIManager.getDefaults().put("TabbedPane.selected", new java.awt.Color(55, 55, 55));
        UIManager.getDefaults().put("TabbedPane.focus", new java.awt.Color(55, 55, 55));
        UIManager.getDefaults().put("TabbedPane.borderHightlightColor", new java.awt.Color(51, 51, 51, 0));
        UIManager.getDefaults().put("TabbedPane.shadow", new java.awt.Color(255, 51, 51, 0));
        UIManager.getDefaults().put("TabbedPane.darkShadow", new java.awt.Color(255, 51, 51, 0));
        UIManager.getDefaults().put("TabbedPane.unselectedTabShadow", new java.awt.Color(255, 51, 51, 0));
        UIManager.getDefaults().put("TabbedPane.selectHighlight", new java.awt.Color(51, 51, 51, 0));
        UIManager.getDefaults().put("TabbedPane.foreground", new java.awt.Color(255, 255, 255));

        setBackground(new Color(51, 51, 51, 50));

        TextPane panel = new TextPane();
        panel.setSize(200, 200);
        panel.setBackground(new Color(51, 51, 51, 100));

        panel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setForeground(new java.awt.Color(255, 255, 255));
        panel.setCaretColor(new java.awt.Color(51, 51, 51, 0));
        panel.setSelectionColor(new java.awt.Color(204, 204, 204));
        panel.setAutoscrolls(true);

        TextPane panel2 = new TextPane();
        panel2.setSize(200, 200);
        panel2.setBackground(new Color(51, 51, 51, 100));

        panel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel2.setForeground(new java.awt.Color(255, 255, 255));
        panel2.setCaretColor(new java.awt.Color(51, 51, 51, 0));
        panel2.setSelectionColor(new java.awt.Color(204, 204, 204));
        panel2.setAutoscrolls(true);

        TextPane panel3 = new TextPane();
        panel3.setSize(200, 200);
        panel3.setBackground(new Color(51, 51, 51, 100));

        panel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel3.setForeground(new java.awt.Color(255, 255, 255));
        panel3.setCaretColor(new java.awt.Color(51, 51, 51, 0));
        panel3.setSelectionColor(new java.awt.Color(204, 204, 204));
        panel3.setAutoscrolls(true);

        TransparentScrollPane scroller1 = new TransparentScrollPane(panel);

        scroller1.setBorder(null);
        scroller1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroller1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scroller1.setAutoscrolls(true);
        scroller1.setOpaque(false);

        TransparentScrollPane scroller2 = new TransparentScrollPane(panel2);

        scroller2.setBorder(null);
        scroller2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroller2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scroller2.setAutoscrolls(true);
        scroller2.setOpaque(false);

        TransparentScrollPane scroller3 = new TransparentScrollPane(panel3);

        scroller3.setBorder(null);
        scroller3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroller3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scroller3.setAutoscrolls(true);
        scroller3.setOpaque(false);
        scroller3.setViewportView(panel3);

        JTabbedPane jTabbedPane = new JTabbedPane();
        jTabbedPane.setBackground(new Color(51, 51, 51, 50));

        jTabbedPane.setBackground(new Color(51, 51, 51, 50));
        jTabbedPane.add(scroller1, "Chat");
        jTabbedPane.add(scroller2, "Links");
        jTabbedPane.add(scroller3, "PMs");

        getContentPane().add(new AlphaContainer(jTabbedPane), java.awt.BorderLayout.CENTER);

        this.setSize(200, 200);
        this.setPreferredSize(new Dimension(200, 200));
        this.setLocationRelativeTo(null);

        pack();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

很抱歉,如果这是一个原始问题,但我现在有点绝望了。我试着在这里列出的每个L&amp; F Customizing for JTabbedPane:

JTabbedPane Customizing

没有结果:

UIManager.put("TabbedPane.contentOpaque", false);
UIManager.getDefaults().put("TabbedPane.contentBorderInsets", new     Insets(0,0,0,0));
UIManager.getDefaults().put("TabbedPane.tabsOverlapBorder", false);
UIManager.getDefaults().put("TabbedPane.borderHightlightColor", new java.awt.Color(51, 51, 51, 0));
UIManager.getDefaults().put("TabbedPane.shadow", new java.awt.Color(255, 51, 51, 0));
UIManager.getDefaults().put("TabbedPane.darkShadow", new java.awt.Color(255, 51, 51, 0));
UIManager.getDefaults().put("TabbedPane.unselectedTabShadow", new java.awt.Color(255, 51, 51, 0));

但这条线,见于此:

http://i.imgur.com/8lcrDiw.png

撑。

我如何摆脱这种情况&#34;最后&#34;边框/阴影?所有其他我可以变得残疾/透明,但最后一个,保持。

1 个答案:

答案 0 :(得分:0)

在另一个post找到了有效的解决方案:

import javax.swing.plaf.basic.BasicTabbedPaneUI;

    jTabbedPane.setUI(new BasicTabbedPaneUI() {
        private final Insets borderInsets = new Insets(0, 0, 0, 0);
        @Override
        protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
        }
        @Override
        protected Insets getContentBorderInsets(int tabPlacement) {
            return borderInsets;
        }
    });