Java 如何将所有JButton的默认鼠标按下背景色覆盖为当前背景色的较深阴影?

Java 如何将所有JButton的默认鼠标按下背景色覆盖为当前背景色的较深阴影?,java,swing,overriding,jbutton,background-color,Java,Swing,Overriding,Jbutton,Background Color,假设我有十个jbutton,每个按钮都明确设置了自己的背景色。 现在,他们都得到了默认的鼠标按下的背景色,但我如何设置它是一个阴影更深的各自的背景色没有这样做一个接一个 我知道我可以覆盖UIManager中的颜色: UIManager.put("Button.select", Color.RED); 我在这里找到的 但这将改变它只有一种颜色。按下时,我所有的按钮都会显示红色背景 有没有一种方法可以让它像: UIManager.put("Button.select&

假设我有十个
jbutton
,每个按钮都明确设置了自己的背景色。 现在,他们都得到了默认的鼠标按下的背景色,但我如何设置它是一个阴影更深的各自的背景色没有这样做一个接一个

我知道我可以覆盖
UIManager
中的颜色:

UIManager.put("Button.select", Color.RED);
我在这里找到的

但这将改变它只有一种颜色。按下时,我所有的按钮都会显示红色背景

有没有一种方法可以让它像:

UIManager.put("Button.select", JButton.getBackground().darker());

我正在努力学习java swing,请容忍我的无知。

一种方法是创建自己的
按钮ui
。为了避免重新发明轮子的麻烦,您可以exend一个子类
ButtonUI

例如
BasicButtonUI
如下:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Objects;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicButtonUI;

public class MainWithBasicButtonUI {
    
    public static class SelectButtonUI extends BasicButtonUI {
        protected Color selectColor;
        
        public void setSelectColor(final Color selectColor) {
            this.selectColor = Objects.requireNonNull(selectColor);
        }
        
        public Color getSelectColor() {
            return selectColor;
        }
        
        @Override
        protected void paintButtonPressed(final Graphics g,
                                          final AbstractButton b){
            if (b.isContentAreaFilled()) {
                Dimension size = b.getSize();
                g.setColor(getSelectColor());
                g.fillRect(0, 0, size.width, size.height);
            }
        }
    }
    
    private static void createAndShowGUI() {
        final int rows = 3, cols = 3;
        final Color[] colors = new Color[]{Color.RED.brighter(), Color.GREEN.brighter(), Color.CYAN};
        final JPanel buttons = new JPanel(new GridLayout(rows, cols, 2, 2));
        final Random rand = new Random();
        for (int i = 0; i < rows * cols; ++i) {
            final JButton b = new JButton("Button");
            b.setBackground(colors[rand.nextInt(colors.length)]);
            final SelectButtonUI ui = new SelectButtonUI();
            ui.setSelectColor(b.getBackground().darker());
            b.setUI(ui);
            buttons.add(b);
        }
        final JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(buttons);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(MainWithBasicButtonUI::createAndShowGUI);
    }
}
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Objects;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalButtonUI;

public class MainWithMetalButtonUI {
    
    public static class SelectButtonUI extends MetalButtonUI {
        public SelectButtonUI() {
            selectColor = super.getSelectColor();
        }
        
        public void setSelectColor(final Color selectColor) {
            this.selectColor = Objects.requireNonNull(selectColor);
        }
        
        @Override
        protected Color getSelectColor() {
            return selectColor;
        }
    }
    
    private static void createAndShowGUI() {
        final int rows = 3, cols = 3;
        final Color[] colors = new Color[]{Color.RED.brighter(), Color.GREEN.brighter(), Color.CYAN};
        final JPanel buttons = new JPanel(new GridLayout(rows, cols, 2, 2));
        final Random rand = new Random();
        for (int i = 0; i < rows * cols; ++i) {
            final JButton b = new JButton("Button");
            b.setBackground(colors[rand.nextInt(colors.length)]);
            final SelectButtonUI ui = new SelectButtonUI();
            ui.setSelectColor(b.getBackground().darker());
            b.setUI(ui);
            buttons.add(b);
        }
        final JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(buttons);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(MainWithMetalButtonUI::createAndShowGUI);
    }
}

唯一的问题是,你所有的按钮总是看起来像金属L&F。

一种方法是创建你自己的
ButtonUI
。为了避免重新发明轮子的麻烦,您可以exend一个子类
ButtonUI

例如
BasicButtonUI
如下:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Objects;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicButtonUI;

public class MainWithBasicButtonUI {
    
    public static class SelectButtonUI extends BasicButtonUI {
        protected Color selectColor;
        
        public void setSelectColor(final Color selectColor) {
            this.selectColor = Objects.requireNonNull(selectColor);
        }
        
        public Color getSelectColor() {
            return selectColor;
        }
        
        @Override
        protected void paintButtonPressed(final Graphics g,
                                          final AbstractButton b){
            if (b.isContentAreaFilled()) {
                Dimension size = b.getSize();
                g.setColor(getSelectColor());
                g.fillRect(0, 0, size.width, size.height);
            }
        }
    }
    
    private static void createAndShowGUI() {
        final int rows = 3, cols = 3;
        final Color[] colors = new Color[]{Color.RED.brighter(), Color.GREEN.brighter(), Color.CYAN};
        final JPanel buttons = new JPanel(new GridLayout(rows, cols, 2, 2));
        final Random rand = new Random();
        for (int i = 0; i < rows * cols; ++i) {
            final JButton b = new JButton("Button");
            b.setBackground(colors[rand.nextInt(colors.length)]);
            final SelectButtonUI ui = new SelectButtonUI();
            ui.setSelectColor(b.getBackground().darker());
            b.setUI(ui);
            buttons.add(b);
        }
        final JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(buttons);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(MainWithBasicButtonUI::createAndShowGUI);
    }
}
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Objects;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalButtonUI;

public class MainWithMetalButtonUI {
    
    public static class SelectButtonUI extends MetalButtonUI {
        public SelectButtonUI() {
            selectColor = super.getSelectColor();
        }
        
        public void setSelectColor(final Color selectColor) {
            this.selectColor = Objects.requireNonNull(selectColor);
        }
        
        @Override
        protected Color getSelectColor() {
            return selectColor;
        }
    }
    
    private static void createAndShowGUI() {
        final int rows = 3, cols = 3;
        final Color[] colors = new Color[]{Color.RED.brighter(), Color.GREEN.brighter(), Color.CYAN};
        final JPanel buttons = new JPanel(new GridLayout(rows, cols, 2, 2));
        final Random rand = new Random();
        for (int i = 0; i < rows * cols; ++i) {
            final JButton b = new JButton("Button");
            b.setBackground(colors[rand.nextInt(colors.length)]);
            final SelectButtonUI ui = new SelectButtonUI();
            ui.setSelectColor(b.getBackground().darker());
            b.setUI(ui);
            buttons.add(b);
        }
        final JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(buttons);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(MainWithMetalButtonUI::createAndShowGUI);
    }
}

唯一的问题是,你所有的按钮总是看起来像金属L&F。

根据你开始使用的颜色,“暗”是主观的,这就是为什么你必须定义每个背景颜色和每个“暗”颜色。一旦你做到了这一点,你就可以在你的例子中写出更暗的方法了。这能回答你的问题吗@显然不是。这与我在上面提供的链接相同。“暗”是基于您开始使用的颜色的主观选择,这就是为什么您必须定义每个背景颜色和每个“暗”颜色的原因。一旦你做到了这一点,你就可以在你的例子中写出更暗的方法了。这能回答你的问题吗@显然不是。这是我上面提供的相同链接。完美!除了
MetalButtonUI
解决方案已聚焦外,无法真正看到两者之间的巨大视觉差异,但这很容易操作。谢谢,太好了!除了
MetalButtonUI
解决方案已聚焦外,无法真正看到两者之间的巨大视觉差异,但这很容易操作。谢谢