丑陋的摇摆按钮背景 [英] Ugly Swing Button Background

查看:53
本文介绍了丑陋的摇摆按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GUI,并且主面板都有透明的背景,所以可以通过它们看到背景图像.问题是,点击在Mac OS上的一个面板上,如果按钮具有圆形边框,则其背景会变成不透明的颜色.我如何避免这种情况?

I have a GUI, and the main panels have transparent backgrounds, so one can see the background image through them. The problem is, clicking on a button on one of this panels, on Mac OS, where buttons have round borders, their backgrounds turn in a not-transparent color. How could I avoid this?

这里有一张图片:

推荐答案

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

Simply make the button transparent using setOpaque(false). Never use a alpha color for a background color, Swing only deals with opaque or not opaque components, it does not know how to deal with alpha based colors.

如果您使用基于Alpha的背景色,Swing将不知道1-假定在绘制组件之前正确准备了 Graphics 上下文,并且2-该组件下方的组件也将需要在组件更改时进行更新.

If you use an alpha based background color, Swing does not know that 1- it's suppose to prepare the Graphics context correctly before painting the component and 2- that the components below the component will also need to be updated when the component changes.

随着UI复杂性的增加和变化的开始(UI的更新),使用alpha背景色会产生随机且烦人的绘画瑕疵.

Using alpha background colors will generate random and annoying paint artifacts as the complexity of the UI increases and the changes begin to occur (the UI is updated)

请参见 AWT和Swing中的绘画执行自定义绘画以获取更多详细信息

See Painting in AWT and Swing and Performing Custom Painting for more details

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 方法并自己绘制,请确保首先将组件标记为透明(而不是不透明)

If you want to paint an translucent background, you will need to override the components paintComponent method and paint it yourself, making sure that the component is marked as transparent (not opaque) first

这篇关于丑陋的摇摆按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆