用setBackground()设置JButton边框?

时间:2016-02-25 21:54:57

标签: java swing jbutton

所以我有一些代码可以改变按钮的背景颜色,但是当我使用代码时,它会设置背景颜色和边框颜色。

Picture

有没有办法不让这件事发生?

谢谢!

代码:

public void highlight(ArrayList<JButton> buttons){
    for (JButton j : buttons) {
    j.setBorder(new JButton().getBorder());
    j.setBackground(Color.GREEN);
    j.setForeground(Color.WHITE);
    j.setOpaque(true);
    j.setBorderPainted(false);
    j.setFocusPainted(false);
    j.setBorderPainted(false);
    }
}

1 个答案:

答案 0 :(得分:2)

好的,所以这是一个黑客版本,并且基于你想要保持当前&#34;外观和感觉的想法。按钮,但想要使用不同的填充颜色

这仅仅适用于&#34;突出显示&#34;按钮上的颜色......

Highlighted buttons

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.kaizen.core.ui.ImageUtilities;

public class Main {

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

    public Main() {
        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 {

        public TestPane() {
            setLayout(new GridBagLayout());
            HighlightButton btn = new HighlightButton("Help");
            btn.setMargin(new Insets(20, 20, 20, 20));
            btn.setHighlight(new Color(255, 0, 0, 64));
            add(btn);

            btn = new HighlightButton("Help");
            btn.setMargin(new Insets(20, 20, 20, 20));
            btn.setHighlight(new Color(0, 255, 0, 64));
            add(btn);

            btn = new HighlightButton("Help");
            btn.setMargin(new Insets(20, 20, 20, 20));
            btn.setHighlight(new Color(0, 0, 255, 64));
            add(btn);

            btn = new HighlightButton("Help");
            btn.setMargin(new Insets(20, 20, 20, 20));
            add(btn);
        }

    }

    public class HighlightButton extends JButton {

        private Color highlight;

        public HighlightButton() {
            setOpaque(false);
        }

        public HighlightButton(String text) {
            super(text);
            setOpaque(false);
        }

        public void setHighlight(Color color) {
            if (color != highlight) {
                Color old = highlight;
                this.highlight = color;
                firePropertyChange("highlight", old, highlight);
                repaint();
            }
        }

        public Color getHighlight() {
            return highlight;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Color highlight = getHighlight();
            if (highlight != null) {
                BufferedImage img = createCompatibleImage(getWidth(), getHeight(), Transparency.TRANSLUCENT);
                Graphics2D g2d = img.createGraphics();
                super.paintComponent(g2d);
                g2d.dispose();

                BufferedImage mask = generateMask(img, getHighlight(), 1f);
                g.drawImage(img, 0, 0, this);
                g.drawImage(mask, 0, 0, this);
            } else {
                super.paintComponent(g);
            }
        }

    }

    public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
        BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
        image.coerceData(true);
        return image;
    }

    public static GraphicsConfiguration getGraphicsConfiguration() {
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    }

    public static BufferedImage generateMask(BufferedImage imgSource, Color color, float alpha) {

        int imgWidth = imgSource.getWidth();
        int imgHeight = imgSource.getHeight();

        BufferedImage imgMask = createCompatibleImage(imgWidth, imgHeight, Transparency.TRANSLUCENT);
        Graphics2D g2 = imgMask.createGraphics();
        applyQualityRenderingHints(g2);

        g2.drawImage(imgSource, 0, 0, null);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha));
        g2.setColor(color);

        g2.fillRect(0, 0, imgSource.getWidth(), imgSource.getHeight());
        g2.dispose();

        return imgMask;

    }

    public static void applyQualityRenderingHints(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    }

}

基本上,它的作用是画出一副掩盖的&#34;图像,以突出显示的颜色着色在按钮顶部。这一点很重要。颜色的alpha值越高,您看到文本的可能性就越小。

我没有在Windows上对此进行测试,因此我无法保证结果。

内容填充由外观委托执行,并且通常忽略了类的颜色属性(是的,我知道,真棒),所以如果你想尝试做一些更强大的东西,你和# 39; d需要定义自己的外观和感觉委托并接管绘画过程,这不是一项简单的任务。

当然,您可以省略外观填充和外观委托使用的边框并自己绘制(覆盖paintComponent方法),但这不会利用外观设置,这是一个你需要决定的平衡行为