使jtoolbar透明

时间:2015-05-20 06:59:31

标签: java swing transparent jtoolbar

我有一个带背景的jPanel,我想在其上添加一个JToolbar 我的问题是,当我添加JToolbar时,它的默认背景很烦,我把它设置为不透明,但没有效果。
我想删除它的默认背景并使其透明 我阅读了以下文章但没有帮助:
JToolbar background image
这是我的代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

public class Toolbar extends JToolBar {

    private JButton manage;
    private JButton add;
    private JButton search;
    private JButton exit;

    public Toolbar() {
        super();

        manage = new JButton();
        add = new JButton();
        search = new JButton();
        exit = new JButton();

        ImageIcon icon = new ImageIcon("pics/Add-01.png");
        Image img = icon.getImage();
        Image newImage = img.getScaledInstance(80, 80, Image.SCALE_SMOOTH);
        add.setIcon(new ImageIcon(newImage));

        setOpaque(false);

        setBackground(Color.RED);

        add(add);
        add(search);
        add(manage);
        add(exit);

    }

}

感谢您的支持 不工作的代码:

public class Toolbar extends JToolBar {

    public Toolbar() {
        setBackground(Color.RED);
        setOpaque(false);
        add(new JButton("add"));
    }

    protected void addImpl(Component comp, Object constraints, int index) {
        super.addImpl(comp, constraints, index);
        if (comp instanceof JButton) {
            ((JButton) comp).setContentAreaFilled(false);
        }
    }

}

1 个答案:

答案 0 :(得分:6)

基本上,按钮仍然是"填充"他们的背景。您可以指示他们不要通过JButton#setContentAreaFilled

绘制其内容(背景)

此示例将JToolBar的背景颜色设置为红色,这样您就可以看到按钮现在是透明的。要使工具栏透明,只需在构造函数

中添加setOpaque(false)即可

Toolbar

public class CustomToolBar extends JToolBar {

    public CustomToolBar() {
        setBackground(Color.RED);
    }

    protected void addImpl(Component comp, Object constraints, int index) {
        super.addImpl(comp, constraints, index);
        if (comp instanceof JButton) {
            ((JButton) comp).setContentAreaFilled(false);
        }
    }

}

扩展示例....

Transparent ToolBar

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

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

                JButton manage = new JButton("Manage");
                JButton add = new JButton("Add");
                JButton search = new JButton("Search");
                JButton exit = new JButton("Exit");
                CustomToolBar tb = new CustomToolBar();
                tb.add(manage);
                tb.add(add);
                tb.add(search);
                tb.add(exit);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestPane());
                frame.add(tb, BorderLayout.NORTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage bgImg;

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                bgImg = ImageIO.read(new File("..."));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

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

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

    }

    public class CustomToolBar extends JToolBar {

        public CustomToolBar() {
            setBorder(new LineBorder(Color.BLACK, 2));
            setOpaque(false);
        }

        @Override
        protected void addImpl(Component comp, Object constraints, int index) {
            super.addImpl(comp, constraints, index);
            if (comp instanceof JButton) {
                ((JButton) comp).setContentAreaFilled(false);
            }
        }

    }

}