JTabbedPane 和它的选项卡透明度将不起作用

JTabbedPane and it's tab transparency won't work

我已经进行了多次检查并研究了如何使单个面板透明以便显示其下方的图像,但在 JTabbedPane 上的面板上没有成功。我在 JTabbedPane 的两个面板上都有 setOpaque(false)setBackground(new Color(0,0,0,20)),并且在 JTabbedPane 本身上也做了同样的事情。但是,我仍然无法显示选项卡式窗格背面的图像。我还缺少什么?

tabbePane.java

package tabbedpanetransparencypractice;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;

public class tabbedPane extends JTabbedPane{

JPanel tab1panel = new JPanel();
JPanel tab2panel = new JPanel();

public tabbedPane(){
    this.setPreferredSize(new Dimension(400,400));
    this.setBackground(new Color(0,0,0,20));
    this.setOpaque(false);
    tab1panel.setBackground(new Color(0,0,0,20));
    tab2panel.setBackground(new Color(0,0,0,20));
    tab1panel.setOpaque(false);
    tab2panel.setOpaque(false);
    this.addTab("Tab 1", tab1panel);
    this.addTab("Tab 2", tab2panel);
}
}

topPanel.java

package tabbedpanetransparencypractice;
import javax.swing.*;
import java.awt.*;

public class topPanel extends JPanel{
Image myBG = new ImageIcon(getClass().getClassLoader().getResource("Assets/loginBg.jpg")).getImage();
    @Override
    public void paintComponent(Graphics g){
        g.drawImage(myBG,0,0,getWidth(),getHeight(),this);
    }

public topPanel(){
    addTabbedPane();
}

public void addTabbedPane(){
    tabbedPane tabbedpane = new tabbedPane();
    this.add(tabbedpane);
}

}

frame.java

package tabbedpanetransparencypractice;
import java.awt.Dimension;
import javax.swing.*;

public class frame extends JFrame{

topPanel myPanel = new topPanel();

public frame(){
    this.setPreferredSize(new Dimension(600,600));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.pack();
    this.setLocationRelativeTo(null);

    addComponents();

}

final void addComponents(){

    this.setContentPane(myPanel);
}
}

main.java

package tabbedpanetransparencypractice;

public class main {

public static void main(String[] args) {
    frame myFrame = new frame();
}
}

这是我得到的输出(我希望 tab1 和 tab2 是透明的以显示下面的 bg 图片)

如有任何帮助,我将不胜感激。谢谢。

您也许可以使用 UIManager.put("TabbedPane.contentAreaColor", new Color(255, 255, 0, 100));

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TransparentTabbedPaneTest {
  public JComponent makeUI() {
    Color bgc = new Color(110, 110, 0, 100);
    Color fgc = new Color(255, 255, 0, 100);

    UIManager.put("TabbedPane.shadow",                fgc);
    UIManager.put("TabbedPane.darkShadow",            fgc);
    UIManager.put("TabbedPane.light",                 fgc);
    UIManager.put("TabbedPane.highlight",             fgc);
    UIManager.put("TabbedPane.tabAreaBackground",     fgc);
    UIManager.put("TabbedPane.unselectedBackground",  fgc);
    UIManager.put("TabbedPane.background",            bgc);
    UIManager.put("TabbedPane.foreground",            Color.WHITE);
    UIManager.put("TabbedPane.focus",                 fgc);
    UIManager.put("TabbedPane.contentAreaColor",      fgc);
    UIManager.put("TabbedPane.selected",              fgc);
    UIManager.put("TabbedPane.selectHighlight",       fgc);
    UIManager.put("TabbedPane.borderHightlightColor", fgc);

    JTabbedPane tabs = new JTabbedPane();

    JPanel tab1panel = new JPanel();
    tab1panel.setBackground(new Color(0, 220, 220, 50));

    JPanel tab2panel = new JPanel();
    tab2panel.setBackground(new Color(220, 0, 0, 50));

    JPanel tab3panel = new JPanel();
    tab3panel.setBackground(new Color(0, 0, 220, 50));

    JCheckBox cb = new JCheckBox("setOpaque(false)");
    cb.setOpaque(false);
    tab3panel.add(cb);
    tab3panel.add(new JCheckBox("setOpaque(true)"));

    tabs.addTab("Tab 1", tab1panel);
    tabs.addTab("Tab 2", tab2panel);
    tabs.addTab("Tab 3", new AlphaContainer(tab3panel));

    JPanel p = new JPanel(new BorderLayout()) {
      private Image myBG = new ImageIcon(getClass().getResource("test.png")).getImage();
      @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(myBG, 0, 0, getWidth(), getHeight(), this);
      }
    };
    p.add(tabs);
    p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new TransparentTabbedPaneTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

//https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/
class AlphaContainer extends JComponent {
  private JComponent component;
  public AlphaContainer(JComponent component) {
    this.component = component;
    setLayout( new BorderLayout() );
    setOpaque( false );
    component.setOpaque( false );
    add( component );
  }
  /**
   *  Paint the background using the background Color of the
   *  contained component
   */
  @Override
  public void paintComponent(Graphics g) {
    g.setColor( component.getBackground() );
    g.fillRect(0, 0, getWidth(), getHeight());
  }
}

首先,Swing 不知道如何处理背景颜色基于 alpha 但不透明的组件。 Swing 只知道如何处理完全不透明和完全透明的组件。

使用基于 alpha 的颜色会生成奇怪且随机的绘画伪像。简单的答案是,您永远不应该将基于 alpha 的颜色应用于组件的背景(唯一的例外是 JFrame - 感谢 Sun :P)

主要的解决办法是,假装。

也就是说,使组件透明 (setOpaque(false)) 并以降低的 alpha 级别绘制背景。然后您可以使用基于 alpha 的颜色(因为组件不再依赖颜色,因为它是透明的),但我倾向于只使用 AlphaComposite,因为它通常更容易控制和操作。

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                BackgroundPane bp = new BackgroundPane();
                bp.setLayout(new BorderLayout());
                bp.setBorder(new EmptyBorder(20, 20, 20, 20));

                SeeThroughTabbedPane tp = new SeeThroughTabbedPane();
                tp.setAlpha(0.5f);
                tp.addTab("Tab 1", new TestPane("I be see through"));
                tp.addTab("Tab 2", new TestPane("But you can't see me"));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(bp);
                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane(String text) {
            setOpaque(false);
            setLayout(new GridBagLayout());
            JLabel label = new JLabel(text);
            label.setForeground(Color.WHITE);
            add(label);
        }

    }

    public class BackgroundPane extends JPanel {

        private BufferedImage bg;

        public BackgroundPane() {
            try {
                bg = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/megatokyo_omnibus_1_3_cover_by_fredrin-d4oupef 50%.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bg != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - bg.getWidth()) / 2;
                int y = (getHeight() - bg.getHeight()) / 2;
                g2d.drawImage(bg, x, y, this);
                g2d.dispose();
            }
        }

    }

    public class SeeThroughTabbedPane extends JTabbedPane {

        private float alpha;

        public SeeThroughTabbedPane() {
            setOpaque(false);
            setAlpha(1f);
        }

        public void setAlpha(float value) {
            if (alpha != value) {
                float old = alpha;
                this.alpha = value;
                firePropertyChange("alpha", old, alpha);
                repaint();
            }
        }

        public float getAlpha() {
            return alpha;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getBackground());
            g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
            super.paintComponent(g);
        }

    }

}

请记住,您添加到仍然不透明的 JTabbedPane 的任何内容都将保持不变,而不是在我的 TestPane 的构造函数中,我将面板的不透明状态设置为 false

您可能还想看看 Painting in AWT and Swing and Performing Custom Painting