丑陋的摇摆按钮背景

时间:2014-11-10 22:14:56

标签: java swing jpanel jbutton transparency

我有一个GUI,主面板有透明背景,所以 人们可以通过它们看到背景图像。问题是,点击一个 其中一个面板上的按钮,在Mac OS上,按钮具有圆形边框,其背景变为不透明的颜色。我怎么能避免这个?

这是一张图片:

enter image description here

2 个答案:

答案 0 :(得分:3)

只需使用setOpaque(false)使按钮透明。永远不要使用alpha颜色作为背景颜色,Swing只处理不透明或不透明的组件,它不知道如何处理基于alpha的颜色。

如果你使用基于alpha的背景颜色,Swing不知道1-它假设在绘制组件之前正确准备Graphics上下文,并且2-组件下方的组件也将需要在组件更改时进行更新。

随着UI的复杂性增加以及更改开始发生(UI更新),使用Alpha背景颜色将生成随机且恼人的绘画工件

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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

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

    public class TestPane extends JPanel {

        private BufferedImage background;

        public TestPane() {
            JButton btn = new JButton("I'm a transparent button");
            btn.setOpaque(false);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(btn, gbc);
            add(new JButton("I'm not a transparent button"), gbc);

            try {
                background = ImageIO.read(new File("C:\\hold\\thumbnails\\issue522.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

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

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

    }

}

如果要绘制半透明背景,则需要覆盖组件paintComponent方法并自行绘制,确保组件首先标记为透明(不透明)

答案 1 :(得分:1)

哦,我刚刚找到了办法:

b.setOpaque(true);
b.setBackground(new Color(0, 0, 0, 0));

完美地工作^^